From 7d58e978fb9f75296ebf06812b6d7f297fcbeddc Mon Sep 17 00:00:00 2001 From: Wang Huan Date: Thu, 30 May 2024 09:28:04 +0000 Subject: [PATCH 01/20] move out pir open test case from deprecated --- test/deprecated/legacy_test/CMakeLists.txt | 75 +-------- .../legacy_test/test_squared_l2_norm_op.py | 148 ------------------ test/legacy_test/CMakeLists.txt | 72 +++++++++ .../legacy_test/test_accuracy_op.py | 0 .../test_add_position_encoding_op.py | 0 .../legacy_test/test_addmm_op.py | 0 .../legacy_test/test_affine_channel_op.py | 0 .../legacy_test/test_affine_grid_op.py | 0 .../legacy_test/test_assign_op.py | 0 .../legacy_test/test_atan2_op.py | 0 .../legacy_test/test_bce_loss.py | 0 .../legacy_test/test_bicubic_interp_op.py | 0 .../legacy_test/test_bilinear_interp_op.py | 0 .../legacy_test/test_bmm_op.py | 0 .../legacy_test/test_cast_op.py | 0 .../legacy_test/test_channel_shuffle.py | 0 .../legacy_test/test_complex_abs.py | 0 .../legacy_test/test_complex_op.py | 0 .../legacy_test/test_complex_variable.py | 0 .../legacy_test/test_complex_view_op.py | 0 .../legacy_test/test_conj_op.py | 0 .../test_conv2d_op_depthwise_conv.py | 0 ...test_conv2d_transpose_op_depthwise_conv.py | 0 .../legacy_test/test_copysign_op.py | 0 .../legacy_test/test_crop_tensor_op.py | 0 .../legacy_test/test_cross_entropy2_op.py | 0 .../legacy_test/test_cross_entropy_op.py | 0 .../legacy_test/test_cummax_op.py | 0 .../legacy_test/test_cumprod_op.py | 0 .../legacy_test/test_deformable_conv_v1_op.py | 0 .../legacy_test/test_determinant_op.py | 0 .../legacy_test/test_diagonal_op.py | 0 .../legacy_test/test_eigh_op.py | 0 .../test_elementwise_heaviside_op.py | 0 .../legacy_test/test_elementwise_mul_op.py | 0 .../legacy_test/test_elementwise_pow_op.py | 0 .../legacy_test/test_expand_v2_op.py | 0 .../legacy_test/test_fill_any_op.py | 0 .../test_fill_diagonal_tensor_op.py | 0 .../test_flatten_contiguous_range_op.py | 0 .../{deprecated => }/legacy_test/test_flip.py | 0 .../legacy_test/test_fmax_op.py | 0 .../legacy_test/test_fmin_op.py | 0 .../legacy_test/test_fold_op.py | 0 .../test_fractional_max_pool2d_op.py | 0 .../legacy_test/test_full_like_op.py | 0 .../legacy_test/test_gammaln_op.py | 0 .../legacy_test/test_gaussian_random_op.py | 0 .../legacy_test/test_graph_send_recv_op.py | 0 .../legacy_test/test_graph_send_ue_recv_op.py | 0 .../legacy_test/test_graph_send_uv_op.py | 0 .../legacy_test/test_grid_sampler_op.py | 0 .../legacy_test/test_gru_op.py | 0 .../legacy_test/test_gru_unit_op.py | 0 .../legacy_test/test_gumbel_softmax_op.py | 0 .../legacy_test/test_hinge_loss_op.py | 0 .../legacy_test/test_huber_loss_op.py | 0 .../legacy_test/test_identity_loss_op.py | 0 .../legacy_test/test_im2sequence_op.py | 0 .../legacy_test/test_imperative_framework.py | 0 ...perative_star_gan_with_gradient_penalty.py | 0 .../legacy_test/test_index_add_op.py | 0 .../legacy_test/test_index_sample_op.py | 0 .../legacy_test/test_index_select_op.py | 0 .../legacy_test/test_input_spec.py | 0 .../legacy_test/test_instance_norm_op_v2.py | 0 .../legacy_test/test_is_integer.py | 0 .../legacy_test/test_kldiv_loss_op.py | 0 .../legacy_test/test_kron_op.py | 0 .../legacy_test/test_kthvalue_op.py | 0 .../legacy_test/test_l1_norm_op.py | 0 .../legacy_test/test_label_smooth_op.py | 0 .../legacy_test/test_lerp_op.py | 0 .../legacy_test/test_lgamma_op.py | 0 .../legacy_test/test_linear_interp_op.py | 0 .../legacy_test/test_linear_interp_v2_op.py | 0 .../test_load_state_dict_from_old_format.py | 0 .../legacy_test/test_log_loss_op.py | 0 .../legacy_test/test_log_softmax.py | 0 .../legacy_test/test_logsumexp.py | 0 .../legacy_test/test_lr_scheduler.py | 0 .../legacy_test/test_lrn_op.py | 0 .../legacy_test/test_lstm_op.py | 0 .../legacy_test/test_lu_unpack_op.py | 0 .../legacy_test/test_masked_scatter.py | 0 .../legacy_test/test_matmul_op.py | 0 .../legacy_test/test_matmul_v2_op.py | 0 .../legacy_test/test_maxout_op.py | 0 .../legacy_test/test_meshgrid_op.py | 0 .../test_modified_huber_loss_op.py | 0 .../legacy_test/test_mul_op.py | 0 .../legacy_test/test_multi_dot_op.py | 0 .../legacy_test/test_mv_op.py | 0 .../legacy_test/test_nearest_interp_op.py | 0 .../legacy_test/test_nearest_interp_v2_op.py | 0 .../legacy_test/test_ops_nms.py | 0 .../legacy_test/test_overlap_add_op.py | 0 .../legacy_test/test_pad3d_op.py | 0 .../test_paddle_save_load_binary.py | 0 .../legacy_test/test_partial_concat_op.py | 0 .../legacy_test/test_partial_sum_op.py | 0 .../legacy_test/test_pixel_shuffle_op.py | 0 .../legacy_test/test_pool3d_op.py | 0 .../legacy_test/test_put_along_axis_op.py | 0 .../legacy_test/test_qr_op.py | 0 .../legacy_test/test_repeat_interleave_op.py | 0 .../legacy_test/test_reshape_op.py | 0 .../legacy_test/test_reverse_op.py | 0 .../legacy_test/test_roi_align_op.py | 0 .../legacy_test/test_roi_pool_op.py | 0 .../legacy_test/test_roll_op.py | 0 .../legacy_test/test_row_conv_op.py | 0 ...est_save_inference_model_conditional_op.py | 0 .../test_save_model_without_var.py | 0 .../legacy_test/test_scatter_op.py | 0 .../legacy_test/test_selu_op.py | 0 .../legacy_test/test_shuffle_channel_op.py | 0 ...entropy_with_logits_grad_with_auto_grad.py | 0 .../legacy_test/test_sign_op.py | 0 .../legacy_test/test_solve_op.py | 0 .../legacy_test/test_spectral_norm_op.py | 0 .../legacy_test/test_split_op.py | 0 .../test_static_save_load_large.py | 0 .../legacy_test/test_stft_op.py | 0 .../legacy_test/test_svd_op.py | 0 .../legacy_test/test_swiglu.py | 0 .../legacy_test/test_temporal_shift_op.py | 0 .../legacy_test/test_top_k_op.py | 0 .../legacy_test/test_top_k_v2_op.py | 0 .../legacy_test/test_trace_op.py | 0 .../legacy_test/test_triangular_solve_op.py | 0 .../legacy_test/test_trilinear_interp_op.py | 0 .../test_trilinear_interp_v2_op.py | 0 .../legacy_test/test_trunc_op.py | 0 .../legacy_test/test_unfold_op.py | 0 .../legacy_test/test_unique_consecutive_op.py | 0 .../legacy_test/test_unpool3d_op.py | 0 .../legacy_test/test_unpool_op.py | 0 .../legacy_test/test_unstack_op.py | 0 .../legacy_test/test_yolov3_loss_op.py | 0 140 files changed, 74 insertions(+), 221 deletions(-) delete mode 100755 test/deprecated/legacy_test/test_squared_l2_norm_op.py rename test/{deprecated => }/legacy_test/test_accuracy_op.py (100%) rename test/{deprecated => }/legacy_test/test_add_position_encoding_op.py (100%) rename test/{deprecated => }/legacy_test/test_addmm_op.py (100%) rename test/{deprecated => }/legacy_test/test_affine_channel_op.py (100%) rename test/{deprecated => }/legacy_test/test_affine_grid_op.py (100%) rename test/{deprecated => }/legacy_test/test_assign_op.py (100%) rename test/{deprecated => }/legacy_test/test_atan2_op.py (100%) rename test/{deprecated => }/legacy_test/test_bce_loss.py (100%) rename test/{deprecated => }/legacy_test/test_bicubic_interp_op.py (100%) rename test/{deprecated => }/legacy_test/test_bilinear_interp_op.py (100%) rename test/{deprecated => }/legacy_test/test_bmm_op.py (100%) rename test/{deprecated => }/legacy_test/test_cast_op.py (100%) rename test/{deprecated => }/legacy_test/test_channel_shuffle.py (100%) rename test/{deprecated => }/legacy_test/test_complex_abs.py (100%) rename test/{deprecated => }/legacy_test/test_complex_op.py (100%) rename test/{deprecated => }/legacy_test/test_complex_variable.py (100%) rename test/{deprecated => }/legacy_test/test_complex_view_op.py (100%) rename test/{deprecated => }/legacy_test/test_conj_op.py (100%) rename test/{deprecated => }/legacy_test/test_conv2d_op_depthwise_conv.py (100%) rename test/{deprecated => }/legacy_test/test_conv2d_transpose_op_depthwise_conv.py (100%) rename test/{deprecated => }/legacy_test/test_copysign_op.py (100%) rename test/{deprecated => }/legacy_test/test_crop_tensor_op.py (100%) rename test/{deprecated => }/legacy_test/test_cross_entropy2_op.py (100%) rename test/{deprecated => }/legacy_test/test_cross_entropy_op.py (100%) rename test/{deprecated => }/legacy_test/test_cummax_op.py (100%) rename test/{deprecated => }/legacy_test/test_cumprod_op.py (100%) rename test/{deprecated => }/legacy_test/test_deformable_conv_v1_op.py (100%) rename test/{deprecated => }/legacy_test/test_determinant_op.py (100%) rename test/{deprecated => }/legacy_test/test_diagonal_op.py (100%) rename test/{deprecated => }/legacy_test/test_eigh_op.py (100%) rename test/{deprecated => }/legacy_test/test_elementwise_heaviside_op.py (100%) rename test/{deprecated => }/legacy_test/test_elementwise_mul_op.py (100%) rename test/{deprecated => }/legacy_test/test_elementwise_pow_op.py (100%) rename test/{deprecated => }/legacy_test/test_expand_v2_op.py (100%) rename test/{deprecated => }/legacy_test/test_fill_any_op.py (100%) rename test/{deprecated => }/legacy_test/test_fill_diagonal_tensor_op.py (100%) rename test/{deprecated => }/legacy_test/test_flatten_contiguous_range_op.py (100%) rename test/{deprecated => }/legacy_test/test_flip.py (100%) rename test/{deprecated => }/legacy_test/test_fmax_op.py (100%) rename test/{deprecated => }/legacy_test/test_fmin_op.py (100%) rename test/{deprecated => }/legacy_test/test_fold_op.py (100%) rename test/{deprecated => }/legacy_test/test_fractional_max_pool2d_op.py (100%) rename test/{deprecated => }/legacy_test/test_full_like_op.py (100%) rename test/{deprecated => }/legacy_test/test_gammaln_op.py (100%) rename test/{deprecated => }/legacy_test/test_gaussian_random_op.py (100%) rename test/{deprecated => }/legacy_test/test_graph_send_recv_op.py (100%) rename test/{deprecated => }/legacy_test/test_graph_send_ue_recv_op.py (100%) rename test/{deprecated => }/legacy_test/test_graph_send_uv_op.py (100%) rename test/{deprecated => }/legacy_test/test_grid_sampler_op.py (100%) rename test/{deprecated => }/legacy_test/test_gru_op.py (100%) rename test/{deprecated => }/legacy_test/test_gru_unit_op.py (100%) rename test/{deprecated => }/legacy_test/test_gumbel_softmax_op.py (100%) rename test/{deprecated => }/legacy_test/test_hinge_loss_op.py (100%) rename test/{deprecated => }/legacy_test/test_huber_loss_op.py (100%) rename test/{deprecated => }/legacy_test/test_identity_loss_op.py (100%) rename test/{deprecated => }/legacy_test/test_im2sequence_op.py (100%) rename test/{deprecated => }/legacy_test/test_imperative_framework.py (100%) rename test/{deprecated => }/legacy_test/test_imperative_star_gan_with_gradient_penalty.py (100%) rename test/{deprecated => }/legacy_test/test_index_add_op.py (100%) rename test/{deprecated => }/legacy_test/test_index_sample_op.py (100%) rename test/{deprecated => }/legacy_test/test_index_select_op.py (100%) rename test/{deprecated => }/legacy_test/test_input_spec.py (100%) rename test/{deprecated => }/legacy_test/test_instance_norm_op_v2.py (100%) rename test/{deprecated => }/legacy_test/test_is_integer.py (100%) rename test/{deprecated => }/legacy_test/test_kldiv_loss_op.py (100%) rename test/{deprecated => }/legacy_test/test_kron_op.py (100%) rename test/{deprecated => }/legacy_test/test_kthvalue_op.py (100%) rename test/{deprecated => }/legacy_test/test_l1_norm_op.py (100%) rename test/{deprecated => }/legacy_test/test_label_smooth_op.py (100%) rename test/{deprecated => }/legacy_test/test_lerp_op.py (100%) rename test/{deprecated => }/legacy_test/test_lgamma_op.py (100%) rename test/{deprecated => }/legacy_test/test_linear_interp_op.py (100%) rename test/{deprecated => }/legacy_test/test_linear_interp_v2_op.py (100%) rename test/{deprecated => }/legacy_test/test_load_state_dict_from_old_format.py (100%) rename test/{deprecated => }/legacy_test/test_log_loss_op.py (100%) rename test/{deprecated => }/legacy_test/test_log_softmax.py (100%) rename test/{deprecated => }/legacy_test/test_logsumexp.py (100%) rename test/{deprecated => }/legacy_test/test_lr_scheduler.py (100%) rename test/{deprecated => }/legacy_test/test_lrn_op.py (100%) rename test/{deprecated => }/legacy_test/test_lstm_op.py (100%) rename test/{deprecated => }/legacy_test/test_lu_unpack_op.py (100%) rename test/{deprecated => }/legacy_test/test_masked_scatter.py (100%) rename test/{deprecated => }/legacy_test/test_matmul_op.py (100%) rename test/{deprecated => }/legacy_test/test_matmul_v2_op.py (100%) rename test/{deprecated => }/legacy_test/test_maxout_op.py (100%) rename test/{deprecated => }/legacy_test/test_meshgrid_op.py (100%) rename test/{deprecated => }/legacy_test/test_modified_huber_loss_op.py (100%) rename test/{deprecated => }/legacy_test/test_mul_op.py (100%) rename test/{deprecated => }/legacy_test/test_multi_dot_op.py (100%) rename test/{deprecated => }/legacy_test/test_mv_op.py (100%) rename test/{deprecated => }/legacy_test/test_nearest_interp_op.py (100%) rename test/{deprecated => }/legacy_test/test_nearest_interp_v2_op.py (100%) rename test/{deprecated => }/legacy_test/test_ops_nms.py (100%) rename test/{deprecated => }/legacy_test/test_overlap_add_op.py (100%) rename test/{deprecated => }/legacy_test/test_pad3d_op.py (100%) rename test/{deprecated => }/legacy_test/test_paddle_save_load_binary.py (100%) rename test/{deprecated => }/legacy_test/test_partial_concat_op.py (100%) rename test/{deprecated => }/legacy_test/test_partial_sum_op.py (100%) rename test/{deprecated => }/legacy_test/test_pixel_shuffle_op.py (100%) rename test/{deprecated => }/legacy_test/test_pool3d_op.py (100%) rename test/{deprecated => }/legacy_test/test_put_along_axis_op.py (100%) rename test/{deprecated => }/legacy_test/test_qr_op.py (100%) rename test/{deprecated => }/legacy_test/test_repeat_interleave_op.py (100%) rename test/{deprecated => }/legacy_test/test_reshape_op.py (100%) rename test/{deprecated => }/legacy_test/test_reverse_op.py (100%) rename test/{deprecated => }/legacy_test/test_roi_align_op.py (100%) rename test/{deprecated => }/legacy_test/test_roi_pool_op.py (100%) rename test/{deprecated => }/legacy_test/test_roll_op.py (100%) rename test/{deprecated => }/legacy_test/test_row_conv_op.py (100%) rename test/{deprecated => }/legacy_test/test_save_inference_model_conditional_op.py (100%) rename test/{deprecated => }/legacy_test/test_save_model_without_var.py (100%) rename test/{deprecated => }/legacy_test/test_scatter_op.py (100%) rename test/{deprecated => }/legacy_test/test_selu_op.py (100%) rename test/{deprecated => }/legacy_test/test_shuffle_channel_op.py (100%) rename test/{deprecated => }/legacy_test/test_sigmoid_cross_entropy_with_logits_grad_with_auto_grad.py (100%) rename test/{deprecated => }/legacy_test/test_sign_op.py (100%) rename test/{deprecated => }/legacy_test/test_solve_op.py (100%) rename test/{deprecated => }/legacy_test/test_spectral_norm_op.py (100%) rename test/{deprecated => }/legacy_test/test_split_op.py (100%) rename test/{deprecated => }/legacy_test/test_static_save_load_large.py (100%) rename test/{deprecated => }/legacy_test/test_stft_op.py (100%) rename test/{deprecated => }/legacy_test/test_svd_op.py (100%) rename test/{deprecated => }/legacy_test/test_swiglu.py (100%) rename test/{deprecated => }/legacy_test/test_temporal_shift_op.py (100%) rename test/{deprecated => }/legacy_test/test_top_k_op.py (100%) rename test/{deprecated => }/legacy_test/test_top_k_v2_op.py (100%) rename test/{deprecated => }/legacy_test/test_trace_op.py (100%) rename test/{deprecated => }/legacy_test/test_triangular_solve_op.py (100%) rename test/{deprecated => }/legacy_test/test_trilinear_interp_op.py (100%) rename test/{deprecated => }/legacy_test/test_trilinear_interp_v2_op.py (100%) rename test/{deprecated => }/legacy_test/test_trunc_op.py (100%) rename test/{deprecated => }/legacy_test/test_unfold_op.py (100%) rename test/{deprecated => }/legacy_test/test_unique_consecutive_op.py (100%) rename test/{deprecated => }/legacy_test/test_unpool3d_op.py (100%) rename test/{deprecated => }/legacy_test/test_unpool_op.py (100%) rename test/{deprecated => }/legacy_test/test_unstack_op.py (100%) rename test/{deprecated => }/legacy_test/test_yolov3_loss_op.py (100%) diff --git a/test/deprecated/legacy_test/CMakeLists.txt b/test/deprecated/legacy_test/CMakeLists.txt index 18891bc1cb65ee..0b2442dfa4b6ee 100644 --- a/test/deprecated/legacy_test/CMakeLists.txt +++ b/test/deprecated/legacy_test/CMakeLists.txt @@ -130,7 +130,6 @@ endif() if(WIN32) list(REMOVE_ITEM TEST_OPS test_complex_matmul) - list(REMOVE_ITEM TEST_OPS test_ops_nms) list(REMOVE_ITEM TEST_OPS test_trt_convert_preln_residual_bias) list(REMOVE_ITEM TEST_OPS test_masked_multihead_attention_op) list(REMOVE_ITEM TEST_OPS test_fused_ec_moe_op) @@ -401,8 +400,6 @@ endfunction() list(REMOVE_ITEM TEST_OPS test_feed_data_check_shape_type) list(REMOVE_ITEM TEST_OPS test_fetch_lod_tensor_array) list(REMOVE_ITEM TEST_OPS test_data_norm_op) -list(REMOVE_ITEM TEST_OPS test_bilinear_interp_op) -list(REMOVE_ITEM TEST_OPS test_nearest_interp_op) list(REMOVE_ITEM TEST_OPS test_imperative_mnist_sorted_gradient) list(REMOVE_ITEM TEST_OPS test_imperative_mnist) list(REMOVE_ITEM TEST_OPS test_layers_deprecated) @@ -452,8 +449,7 @@ 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 test_affine_channel_op test_gather_nd_op test_scatter_op - test_slice_op) +set(TEST_OPS_WITH_GC test_gather_nd_op test_slice_op) foreach(TEST_OP ${TEST_OPS_WITH_GC}) list(REMOVE_ITEM TEST_OPS ${TEST_OP}) @@ -485,10 +481,6 @@ set_tests_properties(test_logcumsumexp_op PROPERTIES TIMEOUT 30) py_test_modules(test_adam_op_multi_thread MODULES test_adam_op ENVS FLAGS_inner_op_parallelism=4) -py_test_modules(test_bilinear_interp_op MODULES test_bilinear_interp_op ENVS - ${GC_ENVS}) -py_test_modules(test_nearest_interp_op MODULES test_nearest_interp_op ENVS - ${GC_ENVS}) py_test_modules(test_imperative_mnist MODULES test_imperative_mnist ENVS FLAGS_cudnn_deterministic=1) py_test_modules( @@ -511,8 +503,6 @@ if((WITH_GPU) AND (WITH_CUDNN_FRONTEND)) test_fused_dot_product_attention_op) endif() -set_tests_properties(test_conv2d_op_depthwise_conv - PROPERTIES LABELS "RUN_TYPE=EXCLUSIVE") set_tests_properties(test_conv2d_api_deprecated PROPERTIES LABELS "RUN_TYPE=EXCLUSIVE") set_tests_properties(test_conv_nn_grad PROPERTIES LABELS "RUN_TYPE=EXCLUSIVE") @@ -644,17 +634,10 @@ endif() set_tests_properties(test_cross_op PROPERTIES TIMEOUT 120) set_tests_properties(test_imperative_lod_tensor_to_selected_rows PROPERTIES TIMEOUT 200) -set_tests_properties(test_lstm_op PROPERTIES TIMEOUT 120) -set_tests_properties(test_imperative_star_gan_with_gradient_penalty - PROPERTIES TIMEOUT 120) -set_tests_properties(test_bicubic_interp_op PROPERTIES TIMEOUT 120) set_tests_properties(test_deformable_conv_op PROPERTIES TIMEOUT 200) -set_tests_properties(test_nearest_interp_op PROPERTIES TIMEOUT 120) set_tests_properties(test_inplace_softmax_with_cross_entropy PROPERTIES TIMEOUT 120) -set_tests_properties(test_cross_entropy2_op PROPERTIES TIMEOUT 120) -set_tests_properties(test_gru_unit_op PROPERTIES TIMEOUT 120) set_tests_properties(test_regularizer_api PROPERTIES TIMEOUT 150) if(NOT WIN32) if(WITH_NV_JETSON) @@ -666,75 +649,45 @@ set_tests_properties(test_bilateral_slice_op PROPERTIES TIMEOUT 120) set_tests_properties(test_fleet_util PROPERTIES TIMEOUT 120) set_tests_properties(test_imperative_transformer_sorted_gradient PROPERTIES TIMEOUT 120) -set_tests_properties(test_matmul_op PROPERTIES TIMEOUT 120) -set_tests_properties(test_nearest_interp_v2_op PROPERTIES TIMEOUT 120) -set_tests_properties(test_trilinear_interp_op PROPERTIES TIMEOUT 120) set_tests_properties(test_static_save_load PROPERTIES TIMEOUT 250) -set_tests_properties(test_paddle_save_load_binary PROPERTIES TIMEOUT 120) -if(WIN32) - set_tests_properties(test_static_save_load_large PROPERTIES TIMEOUT 900) -else() - set_tests_properties(test_static_save_load_large PROPERTIES TIMEOUT 600) -endif() if(WITH_NV_JETSON) set_tests_properties(test_conv3d_transpose_part2_op PROPERTIES TIMEOUT 1200) set_tests_properties(test_layer_norm_op PROPERTIES TIMEOUT 1500) - set_tests_properties(test_pool3d_op PROPERTIES TIMEOUT 1500) else() set_tests_properties(test_conv3d_transpose_part2_op PROPERTIES TIMEOUT 120) set_tests_properties(test_layer_norm_op PROPERTIES TIMEOUT 250) - set_tests_properties(test_pool3d_op PROPERTIES TIMEOUT 150) endif() set_tests_properties(test_imperative_selected_rows_to_lod_tensor PROPERTIES TIMEOUT 200) -set_tests_properties(test_index_select_op PROPERTIES TIMEOUT 120) -set_tests_properties(test_index_add_op PROPERTIES TIMEOUT 120) set_tests_properties(test_argsort_op PROPERTIES TIMEOUT 120) set_tests_properties(test_gather_nd_op PROPERTIES TIMEOUT 120) -set_tests_properties(test_row_conv_op PROPERTIES TIMEOUT 120) -set_tests_properties(test_deformable_conv_v1_op PROPERTIES TIMEOUT 300) set_tests_properties(test_imperative_ptb_rnn_sorted_gradient PROPERTIES TIMEOUT 120) -set_tests_properties(test_crop_tensor_op PROPERTIES TIMEOUT 120) set_tests_properties(test_imperative_ptb_rnn PROPERTIES TIMEOUT 120) -set_tests_properties(test_svd_op PROPERTIES TIMEOUT 80) -set_tests_properties(test_qr_op PROPERTIES TIMEOUT 60) -set_tests_properties(test_trilinear_interp_v2_op PROPERTIES TIMEOUT 120) set_tests_properties(test_masked_select_op PROPERTIES TIMEOUT 120) set_tests_properties(test_sigmoid_cross_entropy_with_logits_op PROPERTIES TIMEOUT 120) set_tests_properties(test_imperative_optimizer_v2 PROPERTIES TIMEOUT 150) -set_tests_properties(test_partial_sum_op PROPERTIES TIMEOUT 120) set_tests_properties(test_sgd_op PROPERTIES TIMEOUT 250) set_tests_properties(test_generator_dataloader_deprecated PROPERTIES TIMEOUT 120) -set_tests_properties(test_partial_concat_op PROPERTIES TIMEOUT 120) set_tests_properties(test_reduce_op PROPERTIES TIMEOUT 500) set_tests_properties(test_conv_nn_grad PROPERTIES TIMEOUT 220) set_tests_properties(test_program_prune_backward PROPERTIES TIMEOUT 120) set_tests_properties(test_imperative_optimizer_v2 PROPERTIES TIMEOUT 250) -set_tests_properties(test_bilinear_interp_op PROPERTIES TIMEOUT 120) set_tests_properties(test_decoupled_py_reader PROPERTIES TIMEOUT 120) set_tests_properties(test_fuse_bn_act_pass PROPERTIES TIMEOUT 120) -set_tests_properties(test_conv2d_op_depthwise_conv PROPERTIES TIMEOUT 120) set_tests_properties(test_conv2d_api_deprecated PROPERTIES TIMEOUT 120) -set_tests_properties(test_elementwise_mul_op PROPERTIES TIMEOUT 120) set_tests_properties(test_dygraph_multi_forward PROPERTIES TIMEOUT 120) set_tests_properties(test_imperative_ocr_attention_model PROPERTIES TIMEOUT 120) set_tests_properties(test_imperative_mnist PROPERTIES TIMEOUT 120) -set_tests_properties(test_gru_op PROPERTIES TIMEOUT 200) set_tests_properties(test_regularizer PROPERTIES TIMEOUT 150) -set_tests_properties(test_matmul_v2_op PROPERTIES TIMEOUT 120) set_tests_properties(test_slice_op PROPERTIES TIMEOUT 120) -set_tests_properties(test_pad3d_op PROPERTIES TIMEOUT 120) set_tests_properties(test_dataloader_keep_order PROPERTIES TIMEOUT 120) set_tests_properties(test_dataloader_unkeep_order PROPERTIES TIMEOUT 120) set_tests_properties(test_reader_reset PROPERTIES TIMEOUT 120) -set_tests_properties(test_cumprod_op PROPERTIES TIMEOUT 300) set_tests_properties(test_split_program PROPERTIES TIMEOUT 120) -set_tests_properties(test_graph_send_ue_recv_op PROPERTIES TIMEOUT 60) -set_tests_properties(test_graph_send_uv_op PROPERTIES TIMEOUT 60) set_tests_properties(test_uniform_random_op_deprecated PROPERTIES TIMEOUT 60) set_tests_properties(test_pretrained_model PROPERTIES TIMEOUT 120) @@ -756,36 +709,18 @@ set_tests_properties(test_inplace_addto_strategy_deprecated PROPERTIES TIMEOUT set(TEST_CINN_OPS test_softmax_op - test_expand_v2_op test_reduce_op test_slice_op - test_full_like_op - test_index_select_op - test_top_k_v2_op - test_elementwise_mul_op test_gather_nd_op - test_elementwise_pow_op test_reshape_op - test_meshgrid_op test_scale_op - test_scatter_op test_layer_norm_op - test_cast_op - test_roll_op - test_atan2_op - test_top_k_op test_where_op test_arg_min_max_op - test_reverse_op - test_flip - test_triangular_solve_op test_scatter_nd_op test_instance_norm_op test_cumsum_op - test_split_op - test_erf_op - test_assign_op - test_flatten_contiguous_range_op) + test_erf_op) foreach(TEST_CINN_OP ${TEST_CINN_OPS}) if(WITH_CINN) @@ -810,16 +745,12 @@ set(STATIC_BUILD_TESTS test_batch_norm_op test_bincount_op test_decoupled_py_reader - test_eigh_op test_fetch_lod_tensor_array test_fuse_bn_act_pass test_layer_norm_op test_lookup_table_v2_op_deprecated - test_matmul_op - test_matmul_v2_op test_momentum_op test_nce - test_paddle_save_load_binary test_reduce_op test_sparse_conv_op test_sparse_norm_op @@ -875,6 +806,4 @@ py_test_modules(test_stride MODULES test_stride ENVS set_tests_properties(test_linalg_matrix_exp PROPERTIES TIMEOUT 120) set_pir_tests_properties() -set_tests_properties(test_fractional_max_pool2d_op PROPERTIES TIMEOUT 120) - set_tests_properties(test_reduce_as_op PROPERTIES TIMEOUT 30) diff --git a/test/deprecated/legacy_test/test_squared_l2_norm_op.py b/test/deprecated/legacy_test/test_squared_l2_norm_op.py deleted file mode 100755 index df36c81097051b..00000000000000 --- a/test/deprecated/legacy_test/test_squared_l2_norm_op.py +++ /dev/null @@ -1,148 +0,0 @@ -# Copyright (c) 2018 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 numpy import linalg as LA -from op_test import OpTest - -import paddle -import paddle.distributed as dist -from paddle import _C_ops, _legacy_C_ops -from paddle.framework import in_dynamic_mode - - -def test_squared_l2_norm(x): - if in_dynamic_mode(): - return _C_ops.squared_l2_norm(x) - else: - return _legacy_C_ops.squared_l2_norm(x) - - -class TestSquaredL2NormF16Op(unittest.TestCase): - def init_test_case(self): - X = np.random.uniform(-0.1, 0.1, (8, 5, 10)).astype('float32') - return X - - def check_main(self, x_np, dtype): - paddle.disable_static() - x = paddle.to_tensor(x_np) - - x.stop_gradient = False - y = test_squared_l2_norm(x) - x_g = paddle.grad(y, [x]) - - paddle.enable_static() - return y, x_g - - def test_main(self): - x_np = self.init_test_case() - y_np_1, x_g_np_1 = self.check_main(x_np, 'float32') - y_np_2, x_g_np_2 = self.check_main(x_np, 'float16') - - def assert_equal(x, y): - np.testing.assert_allclose(x, y, rtol=1e-05, atol=0.0) - - assert_equal(y_np_1, y_np_2) - assert_equal(x_g_np_1, x_g_np_2) - - -class TestSquaredL2NormF16Op1(TestSquaredL2NormF16Op): - def init_test_case(self): - X = np.random.uniform(-2.0, 2.0, (30, 10)).astype('float32') - return X - - -class TestSquaredL2NormF16Op2(TestSquaredL2NormF16Op): - def init_test_case(self): - X = np.random.uniform(-5.0, 5.0, (20, 10, 20)).astype('float32') - return X - - -class TestL2LossOp(OpTest): - """Test squared_l2_norm""" - - def config(self): - self.x_shape = (13, 19) - self.check_auto_parallel = False - - def setUp(self): - self.config() - self.python_api = test_squared_l2_norm - self.op_type = "squared_l2_norm" - self.max_relative_error = 0.05 - - X = np.random.uniform(-1, 1, self.x_shape).astype("float32") - X[np.abs(X) < self.max_relative_error] = 0.1 - self.inputs = {'X': X} - self.outputs = {'Out': np.array([np.square(LA.norm(X))])} - - def test_check_output(self): - self.check_output() - - def test_check_grad(self): - self.check_grad( - ['X'], - 'Out', - max_relative_error=self.max_relative_error, - check_auto_parallel=self.check_auto_parallel, - ) - - -class TestSquaredL2NormAutoParallel_1(TestL2LossOp): - def config(self): - self.x_shape = (14, 18) - self.check_auto_parallel = True - self.placements = { - 'X': [dist.Replicate()], - } - - -class TestSquaredL2NormAutoParallel_2(TestL2LossOp): - def config(self): - self.x_shape = (14, 18) - self.check_auto_parallel = True - self.placements = { - 'X': [dist.Shard(0)], - } - - -class TestSquaredL2NormAutoParallel_3(TestL2LossOp): - def config(self): - self.x_shape = (14, 18) - self.check_auto_parallel = True - self.placements = { - 'X': [dist.Shard(1)], - } - - -class TestL2LossDeterministic(unittest.TestCase): - def check_place(self, 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) - y2 = _legacy_C_ops.squared_l2_norm(x) - np.testing.assert_array_equal(y1.numpy(), y2.numpy()) - - def test_main(self): - self.check_place(paddle.CPUPlace()) - if paddle.is_compiled_with_cuda(): - self.check_place(paddle.CUDAPlace(0)) - - -if __name__ == "__main__": - paddle.enable_static() - unittest.main() diff --git a/test/legacy_test/CMakeLists.txt b/test/legacy_test/CMakeLists.txt index 8c4cfe9113ab3b..9f23c6d4f84cf1 100644 --- a/test/legacy_test/CMakeLists.txt +++ b/test/legacy_test/CMakeLists.txt @@ -155,6 +155,7 @@ if(WIN32) list(REMOVE_ITEM TEST_OPS test_fused_layernorm_op) list(REMOVE_ITEM TEST_OPS test_matmul_int8_op) list(REMOVE_ITEM TEST_OPS test_variable_length_memory_efficient_attention) + list(REMOVE_ITEM TEST_OPS test_ops_nms) endif() list(REMOVE_ITEM TEST_OPS test_checkpoint_saver) @@ -437,6 +438,8 @@ list(REMOVE_ITEM TEST_OPS test_basic_lstm_unit_op) list(REMOVE_ITEM TEST_OPS test_fuse_bn_add_act_pass) list(REMOVE_ITEM TEST_OPS test_conv3d_transpose_op) list(REMOVE_ITEM TEST_OPS test_layers) +list(REMOVE_ITEM TEST_OPS test_bilinear_interp_op) +list(REMOVE_ITEM TEST_OPS test_nearest_interp_op) # disable this unittest temporarily list(REMOVE_ITEM TEST_OPS test_imperative_data_loader_exception) @@ -485,6 +488,8 @@ 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 + test_affine_channel_op + test_scatter_op test_concat_op test_elementwise_add_op test_lookup_table_op @@ -571,6 +576,11 @@ if((WITH_GPU) AND (WITH_CUDNN_FRONTEND)) test_fused_dot_product_attention_op) endif() +py_test_modules(test_bilinear_interp_op MODULES test_bilinear_interp_op ENVS + ${GC_ENVS}) +py_test_modules(test_nearest_interp_op MODULES test_nearest_interp_op ENVS + ${GC_ENVS}) + set_tests_properties(test_conv2d_op PROPERTIES LABELS "RUN_TYPE=EXCLUSIVE") set_tests_properties(test_norm_nn_grad PROPERTIES LABELS "RUN_TYPE=EXCLUSIVE") set_tests_properties(test_nn_grad PROPERTIES LABELS "RUN_TYPE=EXCLUSIVE") @@ -788,12 +798,14 @@ if(WITH_NV_JETSON) set_tests_properties(test_norm_op PROPERTIES TIMEOUT 1200) set_tests_properties(test_batch_norm_op_prim_nchw PROPERTIES TIMEOUT 1500) set_tests_properties(test_batch_norm_op_prim_nhwc PROPERTIES TIMEOUT 1500) + set_tests_properties(test_pool3d_op PROPERTIES TIMEOUT 1500) else() set_tests_properties(test_concat_op PROPERTIES TIMEOUT 400) set_tests_properties(test_conv3d_op PROPERTIES TIMEOUT 120) set_tests_properties(test_norm_op PROPERTIES TIMEOUT 150) set_tests_properties(test_batch_norm_op_prim_nchw PROPERTIES TIMEOUT 250) set_tests_properties(test_batch_norm_op_prim_nhwc PROPERTIES TIMEOUT 250) + set_tests_properties(test_pool3d_op PROPERTIES TIMEOUT 150) endif() if((WITH_GPU OR WITH_ROCM) AND (LINUX)) py_test_modules(test_conv3d_transpose_op MODULES test_conv3d_transpose_op @@ -946,6 +958,9 @@ if(WITH_CUDNN_FRONTEND) endif() set(TEST_CINN_OPS + test_assign_op + test_atan2_op + test_cast_op test_stack_op test_activation_op test_fill_any_like_op @@ -954,6 +969,21 @@ set(TEST_CINN_OPS test_elementwise_sub_op test_elementwise_div_op test_elementwise_max_op + test_elementwise_mul_op + test_elementwise_pow_op + test_expand_v2_op + test_flatten_contiguous_range_op + test_flip + test_full_like_op + test_top_k_op + test_top_k_v2_op + test_triangular_solve_op + test_split_op + test_scatter_op + test_reverse_op + test_roll_op + test_meshgrid_op + test_index_select_op test_mean_op test_clip_op test_gather_op @@ -997,6 +1027,10 @@ set_tests_properties( # 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_adagrad_op + test_eigh_op + test_matmul_op + test_matmul_v2_op + test_paddle_save_load_binary test_assign_pos_op test_bucketize_api test_c_embedding_op @@ -1099,3 +1133,41 @@ set_pir_tests_properties() set_tests_properties(test_nadam_op PROPERTIES TIMEOUT 100) set_tests_properties(test_radam_op PROPERTIES TIMEOUT 100) set_tests_properties(test_nan_inf PROPERTIES TIMEOUT 120) +set_tests_properties(test_bicubic_interp_op PROPERTIES TIMEOUT 120) +set_tests_properties(test_bilinear_interp_op PROPERTIES TIMEOUT 120) +set_tests_properties(test_conv2d_op_depthwise_conv + PROPERTIES LABELS "RUN_TYPE=EXCLUSIVE") +set_tests_properties(test_conv2d_op_depthwise_conv PROPERTIES TIMEOUT 120) +set_tests_properties(test_crop_tensor_op PROPERTIES TIMEOUT 120) +set_tests_properties(test_cross_entropy2_op PROPERTIES TIMEOUT 120) +set_tests_properties(test_cumprod_op PROPERTIES TIMEOUT 300) +set_tests_properties(test_deformable_conv_v1_op PROPERTIES TIMEOUT 300) +set_tests_properties(test_elementwise_mul_op PROPERTIES TIMEOUT 120) +set_tests_properties(test_fractional_max_pool2d_op PROPERTIES TIMEOUT 120) +set_tests_properties(test_graph_send_ue_recv_op PROPERTIES TIMEOUT 60) +set_tests_properties(test_graph_send_uv_op PROPERTIES TIMEOUT 60) +set_tests_properties(test_gru_op PROPERTIES TIMEOUT 200) +set_tests_properties(test_gru_unit_op PROPERTIES TIMEOUT 120) +set_tests_properties(test_imperative_star_gan_with_gradient_penalty + PROPERTIES TIMEOUT 120) +set_tests_properties(test_index_add_op PROPERTIES TIMEOUT 120) +set_tests_properties(test_index_select_op PROPERTIES TIMEOUT 120) +set_tests_properties(test_lstm_op PROPERTIES TIMEOUT 120) +set_tests_properties(test_matmul_op PROPERTIES TIMEOUT 120) +set_tests_properties(test_matmul_v2_op PROPERTIES TIMEOUT 120) +set_tests_properties(test_nearest_interp_op PROPERTIES TIMEOUT 120) +set_tests_properties(test_nearest_interp_v2_op PROPERTIES TIMEOUT 120) +set_tests_properties(test_pad3d_op PROPERTIES TIMEOUT 120) +set_tests_properties(test_paddle_save_load_binary PROPERTIES TIMEOUT 120) +set_tests_properties(test_partial_concat_op PROPERTIES TIMEOUT 120) +set_tests_properties(test_partial_sum_op PROPERTIES TIMEOUT 120) +set_tests_properties(test_qr_op PROPERTIES TIMEOUT 60) +set_tests_properties(test_row_conv_op PROPERTIES TIMEOUT 120) +if(WIN32) + set_tests_properties(test_static_save_load_large PROPERTIES TIMEOUT 900) +else() + set_tests_properties(test_static_save_load_large PROPERTIES TIMEOUT 600) +endif() +set_tests_properties(test_svd_op PROPERTIES TIMEOUT 80) +set_tests_properties(test_trilinear_interp_op PROPERTIES TIMEOUT 120) +set_tests_properties(test_trilinear_interp_v2_op PROPERTIES TIMEOUT 120) diff --git a/test/deprecated/legacy_test/test_accuracy_op.py b/test/legacy_test/test_accuracy_op.py similarity index 100% rename from test/deprecated/legacy_test/test_accuracy_op.py rename to test/legacy_test/test_accuracy_op.py diff --git a/test/deprecated/legacy_test/test_add_position_encoding_op.py b/test/legacy_test/test_add_position_encoding_op.py similarity index 100% rename from test/deprecated/legacy_test/test_add_position_encoding_op.py rename to test/legacy_test/test_add_position_encoding_op.py diff --git a/test/deprecated/legacy_test/test_addmm_op.py b/test/legacy_test/test_addmm_op.py similarity index 100% rename from test/deprecated/legacy_test/test_addmm_op.py rename to test/legacy_test/test_addmm_op.py diff --git a/test/deprecated/legacy_test/test_affine_channel_op.py b/test/legacy_test/test_affine_channel_op.py similarity index 100% rename from test/deprecated/legacy_test/test_affine_channel_op.py rename to test/legacy_test/test_affine_channel_op.py diff --git a/test/deprecated/legacy_test/test_affine_grid_op.py b/test/legacy_test/test_affine_grid_op.py similarity index 100% rename from test/deprecated/legacy_test/test_affine_grid_op.py rename to test/legacy_test/test_affine_grid_op.py diff --git a/test/deprecated/legacy_test/test_assign_op.py b/test/legacy_test/test_assign_op.py similarity index 100% rename from test/deprecated/legacy_test/test_assign_op.py rename to test/legacy_test/test_assign_op.py diff --git a/test/deprecated/legacy_test/test_atan2_op.py b/test/legacy_test/test_atan2_op.py similarity index 100% rename from test/deprecated/legacy_test/test_atan2_op.py rename to test/legacy_test/test_atan2_op.py diff --git a/test/deprecated/legacy_test/test_bce_loss.py b/test/legacy_test/test_bce_loss.py similarity index 100% rename from test/deprecated/legacy_test/test_bce_loss.py rename to test/legacy_test/test_bce_loss.py diff --git a/test/deprecated/legacy_test/test_bicubic_interp_op.py b/test/legacy_test/test_bicubic_interp_op.py similarity index 100% rename from test/deprecated/legacy_test/test_bicubic_interp_op.py rename to test/legacy_test/test_bicubic_interp_op.py diff --git a/test/deprecated/legacy_test/test_bilinear_interp_op.py b/test/legacy_test/test_bilinear_interp_op.py similarity index 100% rename from test/deprecated/legacy_test/test_bilinear_interp_op.py rename to test/legacy_test/test_bilinear_interp_op.py diff --git a/test/deprecated/legacy_test/test_bmm_op.py b/test/legacy_test/test_bmm_op.py similarity index 100% rename from test/deprecated/legacy_test/test_bmm_op.py rename to test/legacy_test/test_bmm_op.py diff --git a/test/deprecated/legacy_test/test_cast_op.py b/test/legacy_test/test_cast_op.py similarity index 100% rename from test/deprecated/legacy_test/test_cast_op.py rename to test/legacy_test/test_cast_op.py diff --git a/test/deprecated/legacy_test/test_channel_shuffle.py b/test/legacy_test/test_channel_shuffle.py similarity index 100% rename from test/deprecated/legacy_test/test_channel_shuffle.py rename to test/legacy_test/test_channel_shuffle.py diff --git a/test/deprecated/legacy_test/test_complex_abs.py b/test/legacy_test/test_complex_abs.py similarity index 100% rename from test/deprecated/legacy_test/test_complex_abs.py rename to test/legacy_test/test_complex_abs.py diff --git a/test/deprecated/legacy_test/test_complex_op.py b/test/legacy_test/test_complex_op.py similarity index 100% rename from test/deprecated/legacy_test/test_complex_op.py rename to test/legacy_test/test_complex_op.py diff --git a/test/deprecated/legacy_test/test_complex_variable.py b/test/legacy_test/test_complex_variable.py similarity index 100% rename from test/deprecated/legacy_test/test_complex_variable.py rename to test/legacy_test/test_complex_variable.py diff --git a/test/deprecated/legacy_test/test_complex_view_op.py b/test/legacy_test/test_complex_view_op.py similarity index 100% rename from test/deprecated/legacy_test/test_complex_view_op.py rename to test/legacy_test/test_complex_view_op.py diff --git a/test/deprecated/legacy_test/test_conj_op.py b/test/legacy_test/test_conj_op.py similarity index 100% rename from test/deprecated/legacy_test/test_conj_op.py rename to test/legacy_test/test_conj_op.py diff --git a/test/deprecated/legacy_test/test_conv2d_op_depthwise_conv.py b/test/legacy_test/test_conv2d_op_depthwise_conv.py similarity index 100% rename from test/deprecated/legacy_test/test_conv2d_op_depthwise_conv.py rename to test/legacy_test/test_conv2d_op_depthwise_conv.py diff --git a/test/deprecated/legacy_test/test_conv2d_transpose_op_depthwise_conv.py b/test/legacy_test/test_conv2d_transpose_op_depthwise_conv.py similarity index 100% rename from test/deprecated/legacy_test/test_conv2d_transpose_op_depthwise_conv.py rename to test/legacy_test/test_conv2d_transpose_op_depthwise_conv.py diff --git a/test/deprecated/legacy_test/test_copysign_op.py b/test/legacy_test/test_copysign_op.py similarity index 100% rename from test/deprecated/legacy_test/test_copysign_op.py rename to test/legacy_test/test_copysign_op.py diff --git a/test/deprecated/legacy_test/test_crop_tensor_op.py b/test/legacy_test/test_crop_tensor_op.py similarity index 100% rename from test/deprecated/legacy_test/test_crop_tensor_op.py rename to test/legacy_test/test_crop_tensor_op.py diff --git a/test/deprecated/legacy_test/test_cross_entropy2_op.py b/test/legacy_test/test_cross_entropy2_op.py similarity index 100% rename from test/deprecated/legacy_test/test_cross_entropy2_op.py rename to test/legacy_test/test_cross_entropy2_op.py diff --git a/test/deprecated/legacy_test/test_cross_entropy_op.py b/test/legacy_test/test_cross_entropy_op.py similarity index 100% rename from test/deprecated/legacy_test/test_cross_entropy_op.py rename to test/legacy_test/test_cross_entropy_op.py diff --git a/test/deprecated/legacy_test/test_cummax_op.py b/test/legacy_test/test_cummax_op.py similarity index 100% rename from test/deprecated/legacy_test/test_cummax_op.py rename to test/legacy_test/test_cummax_op.py diff --git a/test/deprecated/legacy_test/test_cumprod_op.py b/test/legacy_test/test_cumprod_op.py similarity index 100% rename from test/deprecated/legacy_test/test_cumprod_op.py rename to test/legacy_test/test_cumprod_op.py diff --git a/test/deprecated/legacy_test/test_deformable_conv_v1_op.py b/test/legacy_test/test_deformable_conv_v1_op.py similarity index 100% rename from test/deprecated/legacy_test/test_deformable_conv_v1_op.py rename to test/legacy_test/test_deformable_conv_v1_op.py diff --git a/test/deprecated/legacy_test/test_determinant_op.py b/test/legacy_test/test_determinant_op.py similarity index 100% rename from test/deprecated/legacy_test/test_determinant_op.py rename to test/legacy_test/test_determinant_op.py diff --git a/test/deprecated/legacy_test/test_diagonal_op.py b/test/legacy_test/test_diagonal_op.py similarity index 100% rename from test/deprecated/legacy_test/test_diagonal_op.py rename to test/legacy_test/test_diagonal_op.py diff --git a/test/deprecated/legacy_test/test_eigh_op.py b/test/legacy_test/test_eigh_op.py similarity index 100% rename from test/deprecated/legacy_test/test_eigh_op.py rename to test/legacy_test/test_eigh_op.py diff --git a/test/deprecated/legacy_test/test_elementwise_heaviside_op.py b/test/legacy_test/test_elementwise_heaviside_op.py similarity index 100% rename from test/deprecated/legacy_test/test_elementwise_heaviside_op.py rename to test/legacy_test/test_elementwise_heaviside_op.py diff --git a/test/deprecated/legacy_test/test_elementwise_mul_op.py b/test/legacy_test/test_elementwise_mul_op.py similarity index 100% rename from test/deprecated/legacy_test/test_elementwise_mul_op.py rename to test/legacy_test/test_elementwise_mul_op.py diff --git a/test/deprecated/legacy_test/test_elementwise_pow_op.py b/test/legacy_test/test_elementwise_pow_op.py similarity index 100% rename from test/deprecated/legacy_test/test_elementwise_pow_op.py rename to test/legacy_test/test_elementwise_pow_op.py diff --git a/test/deprecated/legacy_test/test_expand_v2_op.py b/test/legacy_test/test_expand_v2_op.py similarity index 100% rename from test/deprecated/legacy_test/test_expand_v2_op.py rename to test/legacy_test/test_expand_v2_op.py diff --git a/test/deprecated/legacy_test/test_fill_any_op.py b/test/legacy_test/test_fill_any_op.py similarity index 100% rename from test/deprecated/legacy_test/test_fill_any_op.py rename to test/legacy_test/test_fill_any_op.py diff --git a/test/deprecated/legacy_test/test_fill_diagonal_tensor_op.py b/test/legacy_test/test_fill_diagonal_tensor_op.py similarity index 100% rename from test/deprecated/legacy_test/test_fill_diagonal_tensor_op.py rename to test/legacy_test/test_fill_diagonal_tensor_op.py diff --git a/test/deprecated/legacy_test/test_flatten_contiguous_range_op.py b/test/legacy_test/test_flatten_contiguous_range_op.py similarity index 100% rename from test/deprecated/legacy_test/test_flatten_contiguous_range_op.py rename to test/legacy_test/test_flatten_contiguous_range_op.py diff --git a/test/deprecated/legacy_test/test_flip.py b/test/legacy_test/test_flip.py similarity index 100% rename from test/deprecated/legacy_test/test_flip.py rename to test/legacy_test/test_flip.py diff --git a/test/deprecated/legacy_test/test_fmax_op.py b/test/legacy_test/test_fmax_op.py similarity index 100% rename from test/deprecated/legacy_test/test_fmax_op.py rename to test/legacy_test/test_fmax_op.py diff --git a/test/deprecated/legacy_test/test_fmin_op.py b/test/legacy_test/test_fmin_op.py similarity index 100% rename from test/deprecated/legacy_test/test_fmin_op.py rename to test/legacy_test/test_fmin_op.py diff --git a/test/deprecated/legacy_test/test_fold_op.py b/test/legacy_test/test_fold_op.py similarity index 100% rename from test/deprecated/legacy_test/test_fold_op.py rename to test/legacy_test/test_fold_op.py diff --git a/test/deprecated/legacy_test/test_fractional_max_pool2d_op.py b/test/legacy_test/test_fractional_max_pool2d_op.py similarity index 100% rename from test/deprecated/legacy_test/test_fractional_max_pool2d_op.py rename to test/legacy_test/test_fractional_max_pool2d_op.py diff --git a/test/deprecated/legacy_test/test_full_like_op.py b/test/legacy_test/test_full_like_op.py similarity index 100% rename from test/deprecated/legacy_test/test_full_like_op.py rename to test/legacy_test/test_full_like_op.py diff --git a/test/deprecated/legacy_test/test_gammaln_op.py b/test/legacy_test/test_gammaln_op.py similarity index 100% rename from test/deprecated/legacy_test/test_gammaln_op.py rename to test/legacy_test/test_gammaln_op.py diff --git a/test/deprecated/legacy_test/test_gaussian_random_op.py b/test/legacy_test/test_gaussian_random_op.py similarity index 100% rename from test/deprecated/legacy_test/test_gaussian_random_op.py rename to test/legacy_test/test_gaussian_random_op.py diff --git a/test/deprecated/legacy_test/test_graph_send_recv_op.py b/test/legacy_test/test_graph_send_recv_op.py similarity index 100% rename from test/deprecated/legacy_test/test_graph_send_recv_op.py rename to test/legacy_test/test_graph_send_recv_op.py diff --git a/test/deprecated/legacy_test/test_graph_send_ue_recv_op.py b/test/legacy_test/test_graph_send_ue_recv_op.py similarity index 100% rename from test/deprecated/legacy_test/test_graph_send_ue_recv_op.py rename to test/legacy_test/test_graph_send_ue_recv_op.py diff --git a/test/deprecated/legacy_test/test_graph_send_uv_op.py b/test/legacy_test/test_graph_send_uv_op.py similarity index 100% rename from test/deprecated/legacy_test/test_graph_send_uv_op.py rename to test/legacy_test/test_graph_send_uv_op.py diff --git a/test/deprecated/legacy_test/test_grid_sampler_op.py b/test/legacy_test/test_grid_sampler_op.py similarity index 100% rename from test/deprecated/legacy_test/test_grid_sampler_op.py rename to test/legacy_test/test_grid_sampler_op.py diff --git a/test/deprecated/legacy_test/test_gru_op.py b/test/legacy_test/test_gru_op.py similarity index 100% rename from test/deprecated/legacy_test/test_gru_op.py rename to test/legacy_test/test_gru_op.py diff --git a/test/deprecated/legacy_test/test_gru_unit_op.py b/test/legacy_test/test_gru_unit_op.py similarity index 100% rename from test/deprecated/legacy_test/test_gru_unit_op.py rename to test/legacy_test/test_gru_unit_op.py diff --git a/test/deprecated/legacy_test/test_gumbel_softmax_op.py b/test/legacy_test/test_gumbel_softmax_op.py similarity index 100% rename from test/deprecated/legacy_test/test_gumbel_softmax_op.py rename to test/legacy_test/test_gumbel_softmax_op.py diff --git a/test/deprecated/legacy_test/test_hinge_loss_op.py b/test/legacy_test/test_hinge_loss_op.py similarity index 100% rename from test/deprecated/legacy_test/test_hinge_loss_op.py rename to test/legacy_test/test_hinge_loss_op.py diff --git a/test/deprecated/legacy_test/test_huber_loss_op.py b/test/legacy_test/test_huber_loss_op.py similarity index 100% rename from test/deprecated/legacy_test/test_huber_loss_op.py rename to test/legacy_test/test_huber_loss_op.py diff --git a/test/deprecated/legacy_test/test_identity_loss_op.py b/test/legacy_test/test_identity_loss_op.py similarity index 100% rename from test/deprecated/legacy_test/test_identity_loss_op.py rename to test/legacy_test/test_identity_loss_op.py diff --git a/test/deprecated/legacy_test/test_im2sequence_op.py b/test/legacy_test/test_im2sequence_op.py similarity index 100% rename from test/deprecated/legacy_test/test_im2sequence_op.py rename to test/legacy_test/test_im2sequence_op.py diff --git a/test/deprecated/legacy_test/test_imperative_framework.py b/test/legacy_test/test_imperative_framework.py similarity index 100% rename from test/deprecated/legacy_test/test_imperative_framework.py rename to test/legacy_test/test_imperative_framework.py diff --git a/test/deprecated/legacy_test/test_imperative_star_gan_with_gradient_penalty.py b/test/legacy_test/test_imperative_star_gan_with_gradient_penalty.py similarity index 100% rename from test/deprecated/legacy_test/test_imperative_star_gan_with_gradient_penalty.py rename to test/legacy_test/test_imperative_star_gan_with_gradient_penalty.py diff --git a/test/deprecated/legacy_test/test_index_add_op.py b/test/legacy_test/test_index_add_op.py similarity index 100% rename from test/deprecated/legacy_test/test_index_add_op.py rename to test/legacy_test/test_index_add_op.py diff --git a/test/deprecated/legacy_test/test_index_sample_op.py b/test/legacy_test/test_index_sample_op.py similarity index 100% rename from test/deprecated/legacy_test/test_index_sample_op.py rename to test/legacy_test/test_index_sample_op.py diff --git a/test/deprecated/legacy_test/test_index_select_op.py b/test/legacy_test/test_index_select_op.py similarity index 100% rename from test/deprecated/legacy_test/test_index_select_op.py rename to test/legacy_test/test_index_select_op.py diff --git a/test/deprecated/legacy_test/test_input_spec.py b/test/legacy_test/test_input_spec.py similarity index 100% rename from test/deprecated/legacy_test/test_input_spec.py rename to test/legacy_test/test_input_spec.py diff --git a/test/deprecated/legacy_test/test_instance_norm_op_v2.py b/test/legacy_test/test_instance_norm_op_v2.py similarity index 100% rename from test/deprecated/legacy_test/test_instance_norm_op_v2.py rename to test/legacy_test/test_instance_norm_op_v2.py diff --git a/test/deprecated/legacy_test/test_is_integer.py b/test/legacy_test/test_is_integer.py similarity index 100% rename from test/deprecated/legacy_test/test_is_integer.py rename to test/legacy_test/test_is_integer.py diff --git a/test/deprecated/legacy_test/test_kldiv_loss_op.py b/test/legacy_test/test_kldiv_loss_op.py similarity index 100% rename from test/deprecated/legacy_test/test_kldiv_loss_op.py rename to test/legacy_test/test_kldiv_loss_op.py diff --git a/test/deprecated/legacy_test/test_kron_op.py b/test/legacy_test/test_kron_op.py similarity index 100% rename from test/deprecated/legacy_test/test_kron_op.py rename to test/legacy_test/test_kron_op.py diff --git a/test/deprecated/legacy_test/test_kthvalue_op.py b/test/legacy_test/test_kthvalue_op.py similarity index 100% rename from test/deprecated/legacy_test/test_kthvalue_op.py rename to test/legacy_test/test_kthvalue_op.py diff --git a/test/deprecated/legacy_test/test_l1_norm_op.py b/test/legacy_test/test_l1_norm_op.py similarity index 100% rename from test/deprecated/legacy_test/test_l1_norm_op.py rename to test/legacy_test/test_l1_norm_op.py diff --git a/test/deprecated/legacy_test/test_label_smooth_op.py b/test/legacy_test/test_label_smooth_op.py similarity index 100% rename from test/deprecated/legacy_test/test_label_smooth_op.py rename to test/legacy_test/test_label_smooth_op.py diff --git a/test/deprecated/legacy_test/test_lerp_op.py b/test/legacy_test/test_lerp_op.py similarity index 100% rename from test/deprecated/legacy_test/test_lerp_op.py rename to test/legacy_test/test_lerp_op.py diff --git a/test/deprecated/legacy_test/test_lgamma_op.py b/test/legacy_test/test_lgamma_op.py similarity index 100% rename from test/deprecated/legacy_test/test_lgamma_op.py rename to test/legacy_test/test_lgamma_op.py diff --git a/test/deprecated/legacy_test/test_linear_interp_op.py b/test/legacy_test/test_linear_interp_op.py similarity index 100% rename from test/deprecated/legacy_test/test_linear_interp_op.py rename to test/legacy_test/test_linear_interp_op.py diff --git a/test/deprecated/legacy_test/test_linear_interp_v2_op.py b/test/legacy_test/test_linear_interp_v2_op.py similarity index 100% rename from test/deprecated/legacy_test/test_linear_interp_v2_op.py rename to test/legacy_test/test_linear_interp_v2_op.py diff --git a/test/deprecated/legacy_test/test_load_state_dict_from_old_format.py b/test/legacy_test/test_load_state_dict_from_old_format.py similarity index 100% rename from test/deprecated/legacy_test/test_load_state_dict_from_old_format.py rename to test/legacy_test/test_load_state_dict_from_old_format.py diff --git a/test/deprecated/legacy_test/test_log_loss_op.py b/test/legacy_test/test_log_loss_op.py similarity index 100% rename from test/deprecated/legacy_test/test_log_loss_op.py rename to test/legacy_test/test_log_loss_op.py diff --git a/test/deprecated/legacy_test/test_log_softmax.py b/test/legacy_test/test_log_softmax.py similarity index 100% rename from test/deprecated/legacy_test/test_log_softmax.py rename to test/legacy_test/test_log_softmax.py diff --git a/test/deprecated/legacy_test/test_logsumexp.py b/test/legacy_test/test_logsumexp.py similarity index 100% rename from test/deprecated/legacy_test/test_logsumexp.py rename to test/legacy_test/test_logsumexp.py diff --git a/test/deprecated/legacy_test/test_lr_scheduler.py b/test/legacy_test/test_lr_scheduler.py similarity index 100% rename from test/deprecated/legacy_test/test_lr_scheduler.py rename to test/legacy_test/test_lr_scheduler.py diff --git a/test/deprecated/legacy_test/test_lrn_op.py b/test/legacy_test/test_lrn_op.py similarity index 100% rename from test/deprecated/legacy_test/test_lrn_op.py rename to test/legacy_test/test_lrn_op.py diff --git a/test/deprecated/legacy_test/test_lstm_op.py b/test/legacy_test/test_lstm_op.py similarity index 100% rename from test/deprecated/legacy_test/test_lstm_op.py rename to test/legacy_test/test_lstm_op.py diff --git a/test/deprecated/legacy_test/test_lu_unpack_op.py b/test/legacy_test/test_lu_unpack_op.py similarity index 100% rename from test/deprecated/legacy_test/test_lu_unpack_op.py rename to test/legacy_test/test_lu_unpack_op.py diff --git a/test/deprecated/legacy_test/test_masked_scatter.py b/test/legacy_test/test_masked_scatter.py similarity index 100% rename from test/deprecated/legacy_test/test_masked_scatter.py rename to test/legacy_test/test_masked_scatter.py diff --git a/test/deprecated/legacy_test/test_matmul_op.py b/test/legacy_test/test_matmul_op.py similarity index 100% rename from test/deprecated/legacy_test/test_matmul_op.py rename to test/legacy_test/test_matmul_op.py diff --git a/test/deprecated/legacy_test/test_matmul_v2_op.py b/test/legacy_test/test_matmul_v2_op.py similarity index 100% rename from test/deprecated/legacy_test/test_matmul_v2_op.py rename to test/legacy_test/test_matmul_v2_op.py diff --git a/test/deprecated/legacy_test/test_maxout_op.py b/test/legacy_test/test_maxout_op.py similarity index 100% rename from test/deprecated/legacy_test/test_maxout_op.py rename to test/legacy_test/test_maxout_op.py diff --git a/test/deprecated/legacy_test/test_meshgrid_op.py b/test/legacy_test/test_meshgrid_op.py similarity index 100% rename from test/deprecated/legacy_test/test_meshgrid_op.py rename to test/legacy_test/test_meshgrid_op.py diff --git a/test/deprecated/legacy_test/test_modified_huber_loss_op.py b/test/legacy_test/test_modified_huber_loss_op.py similarity index 100% rename from test/deprecated/legacy_test/test_modified_huber_loss_op.py rename to test/legacy_test/test_modified_huber_loss_op.py diff --git a/test/deprecated/legacy_test/test_mul_op.py b/test/legacy_test/test_mul_op.py similarity index 100% rename from test/deprecated/legacy_test/test_mul_op.py rename to test/legacy_test/test_mul_op.py diff --git a/test/deprecated/legacy_test/test_multi_dot_op.py b/test/legacy_test/test_multi_dot_op.py similarity index 100% rename from test/deprecated/legacy_test/test_multi_dot_op.py rename to test/legacy_test/test_multi_dot_op.py diff --git a/test/deprecated/legacy_test/test_mv_op.py b/test/legacy_test/test_mv_op.py similarity index 100% rename from test/deprecated/legacy_test/test_mv_op.py rename to test/legacy_test/test_mv_op.py diff --git a/test/deprecated/legacy_test/test_nearest_interp_op.py b/test/legacy_test/test_nearest_interp_op.py similarity index 100% rename from test/deprecated/legacy_test/test_nearest_interp_op.py rename to test/legacy_test/test_nearest_interp_op.py diff --git a/test/deprecated/legacy_test/test_nearest_interp_v2_op.py b/test/legacy_test/test_nearest_interp_v2_op.py similarity index 100% rename from test/deprecated/legacy_test/test_nearest_interp_v2_op.py rename to test/legacy_test/test_nearest_interp_v2_op.py diff --git a/test/deprecated/legacy_test/test_ops_nms.py b/test/legacy_test/test_ops_nms.py similarity index 100% rename from test/deprecated/legacy_test/test_ops_nms.py rename to test/legacy_test/test_ops_nms.py diff --git a/test/deprecated/legacy_test/test_overlap_add_op.py b/test/legacy_test/test_overlap_add_op.py similarity index 100% rename from test/deprecated/legacy_test/test_overlap_add_op.py rename to test/legacy_test/test_overlap_add_op.py diff --git a/test/deprecated/legacy_test/test_pad3d_op.py b/test/legacy_test/test_pad3d_op.py similarity index 100% rename from test/deprecated/legacy_test/test_pad3d_op.py rename to test/legacy_test/test_pad3d_op.py diff --git a/test/deprecated/legacy_test/test_paddle_save_load_binary.py b/test/legacy_test/test_paddle_save_load_binary.py similarity index 100% rename from test/deprecated/legacy_test/test_paddle_save_load_binary.py rename to test/legacy_test/test_paddle_save_load_binary.py diff --git a/test/deprecated/legacy_test/test_partial_concat_op.py b/test/legacy_test/test_partial_concat_op.py similarity index 100% rename from test/deprecated/legacy_test/test_partial_concat_op.py rename to test/legacy_test/test_partial_concat_op.py diff --git a/test/deprecated/legacy_test/test_partial_sum_op.py b/test/legacy_test/test_partial_sum_op.py similarity index 100% rename from test/deprecated/legacy_test/test_partial_sum_op.py rename to test/legacy_test/test_partial_sum_op.py diff --git a/test/deprecated/legacy_test/test_pixel_shuffle_op.py b/test/legacy_test/test_pixel_shuffle_op.py similarity index 100% rename from test/deprecated/legacy_test/test_pixel_shuffle_op.py rename to test/legacy_test/test_pixel_shuffle_op.py diff --git a/test/deprecated/legacy_test/test_pool3d_op.py b/test/legacy_test/test_pool3d_op.py similarity index 100% rename from test/deprecated/legacy_test/test_pool3d_op.py rename to test/legacy_test/test_pool3d_op.py diff --git a/test/deprecated/legacy_test/test_put_along_axis_op.py b/test/legacy_test/test_put_along_axis_op.py similarity index 100% rename from test/deprecated/legacy_test/test_put_along_axis_op.py rename to test/legacy_test/test_put_along_axis_op.py diff --git a/test/deprecated/legacy_test/test_qr_op.py b/test/legacy_test/test_qr_op.py similarity index 100% rename from test/deprecated/legacy_test/test_qr_op.py rename to test/legacy_test/test_qr_op.py diff --git a/test/deprecated/legacy_test/test_repeat_interleave_op.py b/test/legacy_test/test_repeat_interleave_op.py similarity index 100% rename from test/deprecated/legacy_test/test_repeat_interleave_op.py rename to test/legacy_test/test_repeat_interleave_op.py diff --git a/test/deprecated/legacy_test/test_reshape_op.py b/test/legacy_test/test_reshape_op.py similarity index 100% rename from test/deprecated/legacy_test/test_reshape_op.py rename to test/legacy_test/test_reshape_op.py diff --git a/test/deprecated/legacy_test/test_reverse_op.py b/test/legacy_test/test_reverse_op.py similarity index 100% rename from test/deprecated/legacy_test/test_reverse_op.py rename to test/legacy_test/test_reverse_op.py diff --git a/test/deprecated/legacy_test/test_roi_align_op.py b/test/legacy_test/test_roi_align_op.py similarity index 100% rename from test/deprecated/legacy_test/test_roi_align_op.py rename to test/legacy_test/test_roi_align_op.py diff --git a/test/deprecated/legacy_test/test_roi_pool_op.py b/test/legacy_test/test_roi_pool_op.py similarity index 100% rename from test/deprecated/legacy_test/test_roi_pool_op.py rename to test/legacy_test/test_roi_pool_op.py diff --git a/test/deprecated/legacy_test/test_roll_op.py b/test/legacy_test/test_roll_op.py similarity index 100% rename from test/deprecated/legacy_test/test_roll_op.py rename to test/legacy_test/test_roll_op.py diff --git a/test/deprecated/legacy_test/test_row_conv_op.py b/test/legacy_test/test_row_conv_op.py similarity index 100% rename from test/deprecated/legacy_test/test_row_conv_op.py rename to test/legacy_test/test_row_conv_op.py diff --git a/test/deprecated/legacy_test/test_save_inference_model_conditional_op.py b/test/legacy_test/test_save_inference_model_conditional_op.py similarity index 100% rename from test/deprecated/legacy_test/test_save_inference_model_conditional_op.py rename to test/legacy_test/test_save_inference_model_conditional_op.py diff --git a/test/deprecated/legacy_test/test_save_model_without_var.py b/test/legacy_test/test_save_model_without_var.py similarity index 100% rename from test/deprecated/legacy_test/test_save_model_without_var.py rename to test/legacy_test/test_save_model_without_var.py diff --git a/test/deprecated/legacy_test/test_scatter_op.py b/test/legacy_test/test_scatter_op.py similarity index 100% rename from test/deprecated/legacy_test/test_scatter_op.py rename to test/legacy_test/test_scatter_op.py diff --git a/test/deprecated/legacy_test/test_selu_op.py b/test/legacy_test/test_selu_op.py similarity index 100% rename from test/deprecated/legacy_test/test_selu_op.py rename to test/legacy_test/test_selu_op.py diff --git a/test/deprecated/legacy_test/test_shuffle_channel_op.py b/test/legacy_test/test_shuffle_channel_op.py similarity index 100% rename from test/deprecated/legacy_test/test_shuffle_channel_op.py rename to test/legacy_test/test_shuffle_channel_op.py diff --git a/test/deprecated/legacy_test/test_sigmoid_cross_entropy_with_logits_grad_with_auto_grad.py b/test/legacy_test/test_sigmoid_cross_entropy_with_logits_grad_with_auto_grad.py similarity index 100% rename from test/deprecated/legacy_test/test_sigmoid_cross_entropy_with_logits_grad_with_auto_grad.py rename to test/legacy_test/test_sigmoid_cross_entropy_with_logits_grad_with_auto_grad.py diff --git a/test/deprecated/legacy_test/test_sign_op.py b/test/legacy_test/test_sign_op.py similarity index 100% rename from test/deprecated/legacy_test/test_sign_op.py rename to test/legacy_test/test_sign_op.py diff --git a/test/deprecated/legacy_test/test_solve_op.py b/test/legacy_test/test_solve_op.py similarity index 100% rename from test/deprecated/legacy_test/test_solve_op.py rename to test/legacy_test/test_solve_op.py diff --git a/test/deprecated/legacy_test/test_spectral_norm_op.py b/test/legacy_test/test_spectral_norm_op.py similarity index 100% rename from test/deprecated/legacy_test/test_spectral_norm_op.py rename to test/legacy_test/test_spectral_norm_op.py diff --git a/test/deprecated/legacy_test/test_split_op.py b/test/legacy_test/test_split_op.py similarity index 100% rename from test/deprecated/legacy_test/test_split_op.py rename to test/legacy_test/test_split_op.py diff --git a/test/deprecated/legacy_test/test_static_save_load_large.py b/test/legacy_test/test_static_save_load_large.py similarity index 100% rename from test/deprecated/legacy_test/test_static_save_load_large.py rename to test/legacy_test/test_static_save_load_large.py diff --git a/test/deprecated/legacy_test/test_stft_op.py b/test/legacy_test/test_stft_op.py similarity index 100% rename from test/deprecated/legacy_test/test_stft_op.py rename to test/legacy_test/test_stft_op.py diff --git a/test/deprecated/legacy_test/test_svd_op.py b/test/legacy_test/test_svd_op.py similarity index 100% rename from test/deprecated/legacy_test/test_svd_op.py rename to test/legacy_test/test_svd_op.py diff --git a/test/deprecated/legacy_test/test_swiglu.py b/test/legacy_test/test_swiglu.py similarity index 100% rename from test/deprecated/legacy_test/test_swiglu.py rename to test/legacy_test/test_swiglu.py diff --git a/test/deprecated/legacy_test/test_temporal_shift_op.py b/test/legacy_test/test_temporal_shift_op.py similarity index 100% rename from test/deprecated/legacy_test/test_temporal_shift_op.py rename to test/legacy_test/test_temporal_shift_op.py diff --git a/test/deprecated/legacy_test/test_top_k_op.py b/test/legacy_test/test_top_k_op.py similarity index 100% rename from test/deprecated/legacy_test/test_top_k_op.py rename to test/legacy_test/test_top_k_op.py diff --git a/test/deprecated/legacy_test/test_top_k_v2_op.py b/test/legacy_test/test_top_k_v2_op.py similarity index 100% rename from test/deprecated/legacy_test/test_top_k_v2_op.py rename to test/legacy_test/test_top_k_v2_op.py diff --git a/test/deprecated/legacy_test/test_trace_op.py b/test/legacy_test/test_trace_op.py similarity index 100% rename from test/deprecated/legacy_test/test_trace_op.py rename to test/legacy_test/test_trace_op.py diff --git a/test/deprecated/legacy_test/test_triangular_solve_op.py b/test/legacy_test/test_triangular_solve_op.py similarity index 100% rename from test/deprecated/legacy_test/test_triangular_solve_op.py rename to test/legacy_test/test_triangular_solve_op.py diff --git a/test/deprecated/legacy_test/test_trilinear_interp_op.py b/test/legacy_test/test_trilinear_interp_op.py similarity index 100% rename from test/deprecated/legacy_test/test_trilinear_interp_op.py rename to test/legacy_test/test_trilinear_interp_op.py diff --git a/test/deprecated/legacy_test/test_trilinear_interp_v2_op.py b/test/legacy_test/test_trilinear_interp_v2_op.py similarity index 100% rename from test/deprecated/legacy_test/test_trilinear_interp_v2_op.py rename to test/legacy_test/test_trilinear_interp_v2_op.py diff --git a/test/deprecated/legacy_test/test_trunc_op.py b/test/legacy_test/test_trunc_op.py similarity index 100% rename from test/deprecated/legacy_test/test_trunc_op.py rename to test/legacy_test/test_trunc_op.py diff --git a/test/deprecated/legacy_test/test_unfold_op.py b/test/legacy_test/test_unfold_op.py similarity index 100% rename from test/deprecated/legacy_test/test_unfold_op.py rename to test/legacy_test/test_unfold_op.py diff --git a/test/deprecated/legacy_test/test_unique_consecutive_op.py b/test/legacy_test/test_unique_consecutive_op.py similarity index 100% rename from test/deprecated/legacy_test/test_unique_consecutive_op.py rename to test/legacy_test/test_unique_consecutive_op.py diff --git a/test/deprecated/legacy_test/test_unpool3d_op.py b/test/legacy_test/test_unpool3d_op.py similarity index 100% rename from test/deprecated/legacy_test/test_unpool3d_op.py rename to test/legacy_test/test_unpool3d_op.py diff --git a/test/deprecated/legacy_test/test_unpool_op.py b/test/legacy_test/test_unpool_op.py similarity index 100% rename from test/deprecated/legacy_test/test_unpool_op.py rename to test/legacy_test/test_unpool_op.py diff --git a/test/deprecated/legacy_test/test_unstack_op.py b/test/legacy_test/test_unstack_op.py similarity index 100% rename from test/deprecated/legacy_test/test_unstack_op.py rename to test/legacy_test/test_unstack_op.py diff --git a/test/deprecated/legacy_test/test_yolov3_loss_op.py b/test/legacy_test/test_yolov3_loss_op.py similarity index 100% rename from test/deprecated/legacy_test/test_yolov3_loss_op.py rename to test/legacy_test/test_yolov3_loss_op.py From 44f2ff69a2718eccd28e78339ad03aae3ac5c8b2 Mon Sep 17 00:00:00 2001 From: Wang Huan Date: Fri, 31 May 2024 01:07:05 +0000 Subject: [PATCH 02/20] refine --- test/deprecated/legacy_test/CMakeLists.txt | 4 ---- test/legacy_test/CMakeLists.txt | 4 ++++ 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/test/deprecated/legacy_test/CMakeLists.txt b/test/deprecated/legacy_test/CMakeLists.txt index 0b2442dfa4b6ee..43a0ca9e6e8b2b 100644 --- a/test/deprecated/legacy_test/CMakeLists.txt +++ b/test/deprecated/legacy_test/CMakeLists.txt @@ -794,11 +794,7 @@ set_tests_properties( ENVIRONMENT "FLAGS_cudnn_deterministic=1;FLAGS_cudnn_batchnorm_spatial_persistent=1;FLAGS_conv_workspace_size_limit=1000" ) -set_tests_properties(test_matmul_op_static_build PROPERTIES TIMEOUT 120) -set_tests_properties(test_matmul_v2_op_static_build PROPERTIES TIMEOUT 120) set_tests_properties(test_layer_norm_op_static_build PROPERTIES TIMEOUT 1500) -set_tests_properties(test_paddle_save_load_binary_static_build - PROPERTIES TIMEOUT 120) set_tests_properties(test_reduce_op_static_build PROPERTIES TIMEOUT 500) py_test_modules(test_stride MODULES test_stride ENVS FLAGS_use_stride_kernel=true) diff --git a/test/legacy_test/CMakeLists.txt b/test/legacy_test/CMakeLists.txt index 9f23c6d4f84cf1..d2ad219413ee87 100644 --- a/test/legacy_test/CMakeLists.txt +++ b/test/legacy_test/CMakeLists.txt @@ -1171,3 +1171,7 @@ endif() set_tests_properties(test_svd_op PROPERTIES TIMEOUT 80) set_tests_properties(test_trilinear_interp_op PROPERTIES TIMEOUT 120) set_tests_properties(test_trilinear_interp_v2_op PROPERTIES TIMEOUT 120) +set_tests_properties(test_matmul_op_static_build PROPERTIES TIMEOUT 120) +set_tests_properties(test_matmul_v2_op_static_build PROPERTIES TIMEOUT 120) +set_tests_properties(test_paddle_save_load_binary_static_build + PROPERTIES TIMEOUT 120) From e9564feb2609fc581fb307d165dad3d6dc5fe57f Mon Sep 17 00:00:00 2001 From: Wang Huan Date: Fri, 31 May 2024 01:40:24 +0000 Subject: [PATCH 03/20] refine --- test/deprecated/legacy_test/CMakeLists.txt | 1 - test/legacy_test/CMakeLists.txt | 1 + 2 files changed, 1 insertion(+), 1 deletion(-) diff --git a/test/deprecated/legacy_test/CMakeLists.txt b/test/deprecated/legacy_test/CMakeLists.txt index 43a0ca9e6e8b2b..a298606604b55d 100644 --- a/test/deprecated/legacy_test/CMakeLists.txt +++ b/test/deprecated/legacy_test/CMakeLists.txt @@ -712,7 +712,6 @@ set(TEST_CINN_OPS test_reduce_op test_slice_op test_gather_nd_op - test_reshape_op test_scale_op test_layer_norm_op test_where_op diff --git a/test/legacy_test/CMakeLists.txt b/test/legacy_test/CMakeLists.txt index d2ad219413ee87..2f0e62e82df894 100644 --- a/test/legacy_test/CMakeLists.txt +++ b/test/legacy_test/CMakeLists.txt @@ -977,6 +977,7 @@ set(TEST_CINN_OPS test_full_like_op test_top_k_op test_top_k_v2_op + test_reshape_op test_triangular_solve_op test_split_op test_scatter_op From 9d8f47d2b030299d7e26bd5835fb22c584dffa5c Mon Sep 17 00:00:00 2001 From: Wang Huan Date: Fri, 31 May 2024 02:36:54 +0000 Subject: [PATCH 04/20] refine --- .../test_attribute_var_deprecated.py | 107 ++++++++++++++++++ .../legacy_test/test_attribute_var.py | 37 ------ 2 files changed, 107 insertions(+), 37 deletions(-) create mode 100644 test/deprecated/legacy_test/test_attribute_var_deprecated.py rename test/{deprecated => }/legacy_test/test_attribute_var.py (82%) diff --git a/test/deprecated/legacy_test/test_attribute_var_deprecated.py b/test/deprecated/legacy_test/test_attribute_var_deprecated.py new file mode 100644 index 00000000000000..5f09dff9093955 --- /dev/null +++ b/test/deprecated/legacy_test/test_attribute_var_deprecated.py @@ -0,0 +1,107 @@ +# 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 tempfile +import unittest + +import numpy as np + +import paddle +import paddle.inference as paddle_infer +from paddle.base.framework import Program, program_guard + +paddle.enable_static() + + +class UnittestBase(unittest.TestCase): + def setUp(self): + self.temp_dir = tempfile.TemporaryDirectory() + self.init_info() + + def tearDwon(self): + self.temp_dir.cleanup() + + def init_info(self): + self.shapes = None + self.save_path = None + + def path_prefix(self): + return type(self).__name__ + + def infer_prog(self): + config = paddle_infer.Config( + self.save_path + '.pdmodel', self.save_path + '.pdiparams' + ) + config.disable_mkldnn() + predictor = paddle_infer.create_predictor(config) + input_names = predictor.get_input_names() + for i, shape in enumerate(self.shapes): + input_handle = predictor.get_input_handle(input_names[i]) + self.fake_input = np.random.randn(*shape).astype("float32") + input_handle.reshape(shape) + input_handle.copy_from_cpu(self.fake_input) + predictor.run() + output_names = predictor.get_output_names() + res = [] + for out_name in output_names: + output_handle = predictor.get_output_handle(out_name) + output_data = output_handle.copy_to_cpu() + res.append(output_data) + + if len(output_names) == 1: + res = res[0] + + return res + + +class TestDropout(UnittestBase): + def init_info(self): + self.shapes = [[10, 10]] + self.save_path = os.path.join(self.temp_dir.name, 'dropout') + + def test_static(self): + main_prog = Program() + startup_prog = Program() + with program_guard(main_prog, startup_prog): + fc = paddle.nn.Linear(10, 10) + x = paddle.randn(self.shapes[0]) + x.stop_gradient = False + feat = fc(x) + # p is a Variable + p = paddle.randn([1]) + out = paddle.nn.functional.dropout(feat, p=p) + sgd = paddle.optimizer.SGD() + sgd.minimize(paddle.mean(out)) + # test _to_string + self.assertTrue("Var[" in str(main_prog)) + + exe = paddle.static.Executor() + exe.run(startup_prog) + res = exe.run(fetch_list=[x, out]) + # export model + paddle.static.save_inference_model(self.save_path, [x], [out], exe) + + # Test for Inference Predictor + infer_out = self.infer_prog() + self.assertEqual(infer_out.shape, (10, 10)) + + self.assertEqual( + main_prog.block(0).ops[4].all_attrs()['dropout_prob'].name, + p.name, + ) + + +if __name__ == '__main__': + unittest.main() diff --git a/test/deprecated/legacy_test/test_attribute_var.py b/test/legacy_test/test_attribute_var.py similarity index 82% rename from test/deprecated/legacy_test/test_attribute_var.py rename to test/legacy_test/test_attribute_var.py index e06e8a3d80d509..cdae49ba0741a4 100644 --- a/test/deprecated/legacy_test/test_attribute_var.py +++ b/test/legacy_test/test_attribute_var.py @@ -66,43 +66,6 @@ def infer_prog(self): return res -class TestDropout(UnittestBase): - def init_info(self): - self.shapes = [[10, 10]] - self.save_path = os.path.join(self.temp_dir.name, 'dropout') - - def test_static(self): - main_prog = Program() - startup_prog = Program() - with program_guard(main_prog, startup_prog): - fc = paddle.nn.Linear(10, 10) - x = paddle.randn(self.shapes[0]) - x.stop_gradient = False - feat = fc(x) - # p is a Variable - p = paddle.randn([1]) - out = paddle.nn.functional.dropout(feat, p=p) - sgd = paddle.optimizer.SGD() - sgd.minimize(paddle.mean(out)) - # test _to_string - self.assertTrue("Var[" in str(main_prog)) - - exe = paddle.static.Executor() - exe.run(startup_prog) - res = exe.run(fetch_list=[x, out]) - # export model - paddle.static.save_inference_model(self.save_path, [x], [out], exe) - - # Test for Inference Predictor - infer_out = self.infer_prog() - self.assertEqual(infer_out.shape, (10, 10)) - - self.assertEqual( - main_prog.block(0).ops[4].all_attrs()['dropout_prob'].name, - p.name, - ) - - class TestTileTensorList(UnittestBase): def init_info(self): self.shapes = [[2, 3, 4]] From 50cb0778b46ab44fee36bd3fb5737af72bfb55fc Mon Sep 17 00:00:00 2001 From: Wang Huan Date: Fri, 31 May 2024 06:54:18 +0000 Subject: [PATCH 05/20] refine --- test/legacy_test/test_accuracy_op.py | 2 +- test/legacy_test/test_imperative_framework.py | 19 +++++-------------- test/legacy_test/test_input_spec.py | 14 +++++++++++--- 3 files changed, 17 insertions(+), 18 deletions(-) diff --git a/test/legacy_test/test_accuracy_op.py b/test/legacy_test/test_accuracy_op.py index 44c4cfa7c49ac3..bf6d86d10da9e5 100755 --- a/test/legacy_test/test_accuracy_op.py +++ b/test/legacy_test/test_accuracy_op.py @@ -126,7 +126,7 @@ def test_type_errors(self): self.assertRaises(TypeError, paddle.metric.accuracy, x2, label) x3 = paddle.static.data( - name='input', shape=[-1, 2], dtype="float16" + name='input', shape=[-1, 2], dtype="float32" ) paddle.static.accuracy(input=x3, label=label) paddle.metric.accuracy(input=x3, label=label) diff --git a/test/legacy_test/test_imperative_framework.py b/test/legacy_test/test_imperative_framework.py index 01f6d37eed4b1f..b85eeb11df5172 100644 --- a/test/legacy_test/test_imperative_framework.py +++ b/test/legacy_test/test_imperative_framework.py @@ -15,7 +15,6 @@ import unittest import numpy as np -from test_imperative_base import new_program_scope import paddle from paddle import base @@ -53,21 +52,13 @@ def forward(self, inputs): class TestDygraphFramework(unittest.TestCase): - def test_dygraph_backward(self): - with new_program_scope(): - mlp = MLP(input_size=2) - var_inp = paddle.static.data("input", shape=[2, 2], dtype="float32") - out = mlp(var_inp) - try: - out.backward() - raise AssertionError( - "backward should not be usable in static graph mode" - ) - except AssertionError as e: - self.assertTrue(e is not None) - def test_dygraph_to_string(self): np_inp = np.array([[1.0, 2.0], [3.0, 4.0]], dtype=np.float32) with base.dygraph.guard(): var_inp = paddle.to_tensor(np_inp) print(str(var_inp)) + + +if __name__ == '__main__': + paddle.disable_static() + unittest.main() diff --git a/test/legacy_test/test_input_spec.py b/test/legacy_test/test_input_spec.py index 8f86d002da306d..aa649b58ca2a8f 100644 --- a/test/legacy_test/test_input_spec.py +++ b/test/legacy_test/test_input_spec.py @@ -35,9 +35,17 @@ def test_default(self): self.assertIsNone(tensor_spec.name) def test_from_tensor(self): - x_bool = paddle.tensor.fill_constant( - shape=[1], dtype='bool', value=True - ) + if paddle.framework.use_pir_api(): + x_bool = paddle.pir.core.create_parameter( + dtype='float32', + shape=[1], + name='xx', + initializer=paddle.nn.initializer.Uniform(), + ) + else: + x_bool = paddle.tensor.fill_constant( + shape=[1], dtype='bool', value=True + ) bool_spec = InputSpec.from_tensor(x_bool) self.assertEqual(bool_spec.dtype, x_bool.dtype) self.assertEqual(list(bool_spec.shape), list(x_bool.shape)) From 8d0d0158b9a4063193a09fb560a9f7004250e7b1 Mon Sep 17 00:00:00 2001 From: Wang Huan Date: Mon, 3 Jun 2024 08:24:21 +0000 Subject: [PATCH 06/20] deprecated old code --- test/contrib/CMakeLists.txt | 1 + test/contrib/test_bf16_utils.py | 111 ++++ .../contrib/test_image_classification_fp16.py | 198 +++++++ test/deprecated/asp/CMakeLists.txt | 4 +- .../asp/test_asp_customized_pruning.py | 157 ------ .../test_asp_customized_pruning_deprecated.py | 205 +++++++ ...=> test_asp_optimize_static_deprecated.py} | 0 ... => test_asp_pruning_static_deprecated.py} | 0 test/deprecated/asp/test_asp_save_load.py | 86 --- .../asp/test_asp_save_load_deprecated.py | 131 +++++ .../collective/fleet/CMakeLists.txt | 7 +- ...16_allreduce_meta_optimizer_deprecated.py} | 0 ...t_fleet_meta_optimizer_base_deprecated.py} | 2 + test/deprecated/contrib/CMakeLists.txt | 3 +- ...utils.py => test_bf16_utils_deprecated.py} | 86 --- ...t_image_classification_fp16_deprecated.py} | 151 ----- ...eltwise_layernorm_fuse_pass_deprecated.py} | 2 + ....py => test_ir_fc_fuse_pass_deprecated.py} | 2 + ...py => test_ir_generate_pass_deprecated.py} | 0 ...st_ir_graph_to_program_pass_deprecated.py} | 0 ...eln_residual_bias_fuse_pass_deprecated.py} | 0 ...test_ir_skip_layernorm_pass_deprecated.py} | 0 ...py => test_ir_yolo_box_pass_deprecated.py} | 0 ...test_op_input_grad_semantic_deprecated.py} | 0 test/deprecated/legacy_test/CMakeLists.txt | 144 +++-- .../run_server_for_communicator_geo.py | 5 +- test/deprecated/legacy_test/test_adam_op.py | 33 -- .../legacy_test/test_adam_op_deprecated.py | 60 ++ .../legacy_test/test_adamax_api_deprecated.py | 54 ++ test/deprecated/legacy_test/test_adamw_op.py | 35 -- .../legacy_test/test_adamw_op_deprecated.py | 61 ++ ... test_add_reader_dependency_deprecated.py} | 2 + .../legacy_test/test_argsort_op_deprecated.py | 355 ++++++++++++ ...t_auto_parallel_partitioner_deprecated.py} | 0 ...to_parallel_partitioner_gpt_deprecated.py} | 0 ..._avoid_twice_initialization_deprecated.py} | 2 + ...ackward.py => test_backward_deprecated.py} | 53 -- .../test_batch_norm_op_deprecated.py | 526 ++++++++++++++++++ ..._bilinear_tensor_product_op_deprecated.py} | 34 +- ...py => test_communicator_geo_deprecated.py} | 0 .../legacy_test/test_compiled_program.py | 42 -- .../test_compiled_program_deprecated.py | 70 +++ ...y => test_conditional_block_deprecated.py} | 0 ...t_conv3d_transpose_part2_op_deprecated.py} | 83 --- .../deprecated/legacy_test/test_cost_model.py | 20 - .../legacy_test/test_cost_model_deprecated.py | 48 ++ .../test_data_norm_op_deprecated.py | 60 ++ ...test_dataloader_early_reset_deprecated.py} | 2 + ... test_dataloader_keep_order_deprecated.py} | 2 + ...est_dataloader_unkeep_order_deprecated.py} | 2 + test/deprecated/legacy_test/test_dataset.py | 142 ----- .../legacy_test/test_dataset_deprecated.py | 172 ++++++ ...=> test_decoupled_py_reader_deprecated.py} | 2 + ...2d.py => test_deform_conv2d_deprecated.py} | 321 ----------- .../test_deformable_conv_op_deprecated.py | 178 ++++++ ...clone.py => test_desc_clone_deprecated.py} | 0 ...ard.py => test_device_guard_deprecated.py} | 63 --- ...a_sync_optimizer_auto_async_deprecated.py} | 0 ...fleet_a_sync_optimizer_auto_deprecated.py} | 0 ...t_a_sync_optimizer_auto_geo_deprecated.py} | 0 ...py => test_dist_fleet_decay_deprecated.py} | 0 ...st_dist_fleet_heter_program_deprecated.py} | 0 ....py => test_dist_fleet_ps10_deprecated.py} | 0 ....py => test_dist_fleet_ps13_deprecated.py} | 0 ...2.py => test_dist_fleet_ps2_deprecated.py} | 0 ...3.py => test_dist_fleet_ps3_deprecated.py} | 0 ...4.py => test_dist_fleet_ps4_deprecated.py} | 0 ...5.py => test_dist_fleet_ps5_deprecated.py} | 0 ...6.py => test_dist_fleet_ps6_deprecated.py} | 0 ...7.py => test_dist_fleet_ps7_deprecated.py} | 0 ...8.py => test_dist_fleet_ps8_deprecated.py} | 0 ...9.py => test_dist_fleet_ps9_deprecated.py} | 0 ...ps.py => test_dist_fleet_ps_deprecated.py} | 0 ...dist_sparse_tensor_load_sgd_deprecated.py} | 0 ..._eager_deletion_delete_vars_deprecated.py} | 0 .../{test_ema.py => test_ema_deprecated.py} | 2 + ..._fleet.py => test_ema_fleet_deprecated.py} | 0 ..._embedding_id_stop_gradient_deprecated.py} | 2 + ...ttr2.py => test_entry_attr2_deprecated.py} | 0 ..._attr.py => test_entry_attr_deprecated.py} | 0 ..._clip.py => test_error_clip_deprecated.py} | 0 ...ecutor_and_use_program_cache_deprecated.py | 88 +++ ...=> test_executor_check_feed_deprecated.py} | 2 + .../test_executor_feed_non_tensor.py | 120 ---- ...est_executor_feed_non_tensor_deprecated.py | 144 +++++ ...test_fc_op.py => test_fc_op_deprecated.py} | 84 +-- ..._feed_data_check_shape_type_deprecated.py} | 2 + .../deprecated/legacy_test/test_fleet_base.py | 41 -- ...e_2.py => test_fleet_base_2_deprecated.py} | 0 ...e_3.py => test_fleet_base_3_deprecated.py} | 0 .../legacy_test/test_fleet_base_deprecated.py | 66 +++ ...test_fleet.py => test_fleet_deprecated.py} | 4 + ..._1.py => test_fleet_nocvm_1_deprecated.py} | 2 + ... => test_fleet_unitaccessor_deprecated.py} | 2 + ...y => test_functional_conv2d_deprecated.py} | 214 ------- ...functional_conv2d_transpose_deprecated.py} | 2 + ...y => test_functional_conv3d_deprecated.py} | 195 ------- .../test_functional_conv3d_transpose.py | 346 ------------ ..._functional_conv3d_transpose_deprecated.py | 405 ++++++++++++++ ...py => test_fuse_bn_act_pass_deprecated.py} | 2 + ...get_inputs_outputs_in_block_deprecated.py} | 0 ...ip.py => test_gradient_clip_deprecated.py} | 0 .../test_hsigmoid_op_deprecated.py | 113 ++++ .../test_image_classification_layer.py | 15 - ...t_image_classification_layer_deprecated.py | 60 ++ .../test_imperative_double_grad.py | 15 - .../test_imperative_double_grad_deprecated.py | 38 ++ ...mperative_load_static_param_deprecated.py} | 0 ...lod_tensor_to_selected_rows_deprecated.py} | 0 ..._infer_no_need_buffer_slots_deprecated.py} | 0 ...pi.py => test_inference_api_deprecated.py} | 44 -- ... => test_inference_model_io_deprecated.py} | 16 - .../legacy_test/test_initializer.py | 76 --- .../test_initializer_deprecated.py | 101 ++++ .../legacy_test/test_instance_norm_op.py | 180 +----- .../test_instance_norm_op_deprecated.py | 271 +++++++++ .../test_layer_norm_op_deprecated.py | 387 +++++++++++++ .../legacy_test/test_math_op_patch.py | 28 - .../test_math_op_patch_deprecated.py | 60 ++ ... => test_merged_momentum_op_deprecated.py} | 0 .../legacy_test/test_momentum_op.py | 75 --- .../test_momentum_op_deprecated.py | 157 ++++++ ...scope.py => test_name_scope_deprecated.py} | 2 + .../{test_nce.py => test_nce_deprecated.py} | 90 +-- ...imizer.py => test_optimizer_deprecated.py} | 141 ----- ...t_optimizer_in_control_flow_deprecated.py} | 0 .../legacy_test/test_prelu_op_deprecated.py | 86 +++ ...ode.py => test_program_code_deprecated.py} | 0 ..._program.py => test_program_deprecated.py} | 0 ...test_program_prune_backward_deprecated.py} | 0 ...y => test_program_to_string_deprecated.py} | 2 + ...test_prune.py => test_prune_deprecated.py} | 2 + ...nc_op.py => test_py_func_op_deprecated.py} | 2 + .../test_random_seed_deprecated.py | 82 +++ .../test_regularizer_api_deprecated.py | 180 ++++++ ...izer.py => test_regularizer_deprecated.py} | 92 --- ...p.py => test_run_program_op_deprecated.py} | 0 ...test_select_input_output_op_deprecated.py} | 0 .../legacy_test/test_sgd_op_deprecated.py | 214 +++++++ test/deprecated/legacy_test/test_slice_op.py | 9 - .../legacy_test/test_slice_op_deprecated.py | 37 ++ ...> test_static_pylayer_block_deprecated.py} | 0 ...r.py => test_static_pylayer_deprecated.py} | 5 +- ...st_switch.py => test_switch_deprecated.py} | 2 + .../test_tensor_array_to_tensor.py | 116 ---- .../test_tensor_array_to_tensor_deprecated.py | 143 +++++ ...inable.py => test_trainable_deprecated.py} | 0 ...runcated_gaussian_random_op_deprecated.py} | 0 test/deprecated/legacy_test/test_variable.py | 120 ---- .../legacy_test/test_variable_deprecated.py | 162 ++++++ ...> test_weight_normalization_deprecated.py} | 2 + ...> test_composite_layer_norm_deprecated.py} | 0 ...t_grad_op_desc_prim_enabled_deprecated.py} | 0 ...py => test_comp_skip_op_set_deprecated.py} | 0 test/deprecated/quantization/CMakeLists.txt | 24 +- ...test_graph.py => test_graph_deprecated.py} | 0 ...ng_average_abs_max_scale_op_deprecated.py} | 0 ...est_quant2_int8_mkldnn_pass_deprecated.py} | 0 .../deprecated/quantization/test_quant_amp.py | 2 +- ...ware.py => test_quant_aware_deprecated.py} | 0 .../test_quant_aware_user_defined.py | 5 +- .../test_quant_post_quant_aware.py | 2 +- ...st_quantization_mkldnn_pass_deprecated.py} | 0 ...y => test_quantization_pass_deprecated.py} | 0 ...est_quantization_scale_pass_deprecated.py} | 0 ...t_user_defined_quantization_deprecated.py} | 0 test/deprecated/rnn/CMakeLists.txt | 2 + test/deprecated/rnn/test_rnn_nets.py | 76 --- .../rnn/test_rnn_nets_deprecated.py | 327 +++++++++++ test/deprecated/rnn/test_rnn_nets_static.py | 123 ---- .../rnn/test_rnn_nets_static_deprecated.py | 369 ++++++++++++ ..._dist_attr_run_time_set_get_deprecated.py} | 0 .../test_standalone_executor.py | 115 ---- .../test_standalone_executor_deprecated.py | 175 ++++++ ..._executor_multi_micro_batch_deprecated.py} | 0 ...st_standalone_executor_plan_deprecated.py} | 0 ...test_standalone_op_priority_deprecated.py} | 0 ...t_standalone_sequentail_run_deprecated.py} | 0 test/deprecated/tokenizer/CMakeLists.txt | 4 +- ...=> test_faster_tokenizer_op_deprecated.py} | 0 test/legacy_test/CMakeLists.txt | 20 +- .../legacy_test/test_adamax_api.py | 29 - .../legacy_test/test_argsort_op.py | 290 ---------- test/legacy_test/test_backward.py | 106 ++++ .../legacy_test/test_batch_norm_op.py | 398 +------------ .../test_bilinear_tensor_product_op.py | 57 ++ .../test_conv3d_transpose_part2_op.py | 104 ++++ .../legacy_test/test_conv_nn_grad.py | 374 ------------- .../legacy_test/test_data_norm_op.py | 36 +- test/legacy_test/test_deform_conv2d.py | 346 ++++++++++++ .../legacy_test/test_deformable_conv_op.py | 67 --- test/legacy_test/test_device_guard.py | 106 ++++ .../test_executor_and_use_program_cache.py | 60 -- test/legacy_test/test_fc_op.py | 136 +++++ test/legacy_test/test_functional_conv2d.py | 284 ++++++++++ test/legacy_test/test_functional_conv3d.py | 265 +++++++++ .../legacy_test/test_hsigmoid_op.py | 86 --- test/legacy_test/test_inference_api.py | 69 +++ test/legacy_test/test_inference_model_io.py | 45 ++ .../legacy_test/test_layer_norm_op.py | 263 --------- test/legacy_test/test_nce.py | 154 +++++ test/legacy_test/test_optimizer.py | 169 ++++++ .../legacy_test/test_prelu_op.py | 62 +-- .../legacy_test/test_random_seed.py | 51 -- test/legacy_test/test_regularizer.py | 217 ++++++++ .../legacy_test/test_regularizer_api.py | 57 -- .../legacy_test/test_sgd_op.py | 179 ------ 207 files changed, 8156 insertions(+), 5663 deletions(-) create mode 100644 test/contrib/test_bf16_utils.py create mode 100644 test/contrib/test_image_classification_fp16.py create mode 100644 test/deprecated/asp/test_asp_customized_pruning_deprecated.py rename test/deprecated/asp/{test_asp_optimize_static.py => test_asp_optimize_static_deprecated.py} (100%) rename test/deprecated/asp/{test_asp_pruning_static.py => test_asp_pruning_static_deprecated.py} (100%) create mode 100644 test/deprecated/asp/test_asp_save_load_deprecated.py rename test/deprecated/collective/fleet/{test_fleet_fp16_allreduce_meta_optimizer.py => test_fleet_fp16_allreduce_meta_optimizer_deprecated.py} (100%) rename test/deprecated/collective/fleet/{test_fleet_meta_optimizer_base.py => test_fleet_meta_optimizer_base_deprecated.py} (99%) rename test/deprecated/contrib/{test_bf16_utils.py => test_bf16_utils_deprecated.py} (57%) rename test/deprecated/contrib/{test_image_classification_fp16.py => test_image_classification_fp16_deprecated.py} (70%) rename test/deprecated/ir/{test_ir_embedding_eltwise_layernorm_fuse_pass.py => test_ir_embedding_eltwise_layernorm_fuse_pass_deprecated.py} (99%) rename test/deprecated/ir/{test_ir_fc_fuse_pass.py => test_ir_fc_fuse_pass_deprecated.py} (98%) rename test/deprecated/ir/{test_ir_generate_pass.py => test_ir_generate_pass_deprecated.py} (100%) rename test/deprecated/ir/{test_ir_graph_to_program_pass.py => test_ir_graph_to_program_pass_deprecated.py} (100%) rename test/deprecated/ir/{test_ir_preln_residual_bias_fuse_pass.py => test_ir_preln_residual_bias_fuse_pass_deprecated.py} (100%) rename test/deprecated/ir/{test_ir_skip_layernorm_pass.py => test_ir_skip_layernorm_pass_deprecated.py} (100%) rename test/deprecated/ir/{test_ir_yolo_box_pass.py => test_ir_yolo_box_pass_deprecated.py} (100%) rename test/deprecated/ir/{test_op_input_grad_semantic.py => test_op_input_grad_semantic_deprecated.py} (100%) create mode 100644 test/deprecated/legacy_test/test_adam_op_deprecated.py create mode 100644 test/deprecated/legacy_test/test_adamax_api_deprecated.py create mode 100644 test/deprecated/legacy_test/test_adamw_op_deprecated.py rename test/deprecated/legacy_test/{test_add_reader_dependency.py => test_add_reader_dependency_deprecated.py} (99%) create mode 100644 test/deprecated/legacy_test/test_argsort_op_deprecated.py rename test/deprecated/legacy_test/{test_auto_parallel_partitioner.py => test_auto_parallel_partitioner_deprecated.py} (100%) rename test/deprecated/legacy_test/{test_auto_parallel_partitioner_gpt.py => test_auto_parallel_partitioner_gpt_deprecated.py} (100%) rename test/deprecated/legacy_test/{test_avoid_twice_initialization.py => test_avoid_twice_initialization_deprecated.py} (98%) rename test/deprecated/legacy_test/{test_backward.py => test_backward_deprecated.py} (88%) create mode 100644 test/deprecated/legacy_test/test_batch_norm_op_deprecated.py rename test/deprecated/legacy_test/{test_bilinear_tensor_product_op.py => test_bilinear_tensor_product_op_deprecated.py} (64%) rename test/deprecated/legacy_test/{test_communicator_geo.py => test_communicator_geo_deprecated.py} (100%) create mode 100644 test/deprecated/legacy_test/test_compiled_program_deprecated.py rename test/deprecated/legacy_test/{test_conditional_block.py => test_conditional_block_deprecated.py} (100%) rename test/deprecated/legacy_test/{test_conv3d_transpose_part2_op.py => test_conv3d_transpose_part2_op_deprecated.py} (65%) create mode 100644 test/deprecated/legacy_test/test_cost_model_deprecated.py create mode 100644 test/deprecated/legacy_test/test_data_norm_op_deprecated.py rename test/deprecated/legacy_test/{test_dataloader_early_reset.py => test_dataloader_early_reset_deprecated.py} (99%) rename test/deprecated/legacy_test/{test_dataloader_keep_order.py => test_dataloader_keep_order_deprecated.py} (99%) rename test/deprecated/legacy_test/{test_dataloader_unkeep_order.py => test_dataloader_unkeep_order_deprecated.py} (99%) create mode 100644 test/deprecated/legacy_test/test_dataset_deprecated.py rename test/deprecated/legacy_test/{test_decoupled_py_reader.py => test_decoupled_py_reader_deprecated.py} (99%) rename test/deprecated/legacy_test/{test_deform_conv2d.py => test_deform_conv2d_deprecated.py} (57%) create mode 100644 test/deprecated/legacy_test/test_deformable_conv_op_deprecated.py rename test/deprecated/legacy_test/{test_desc_clone.py => test_desc_clone_deprecated.py} (100%) rename test/deprecated/legacy_test/{test_device_guard.py => test_device_guard_deprecated.py} (75%) rename test/deprecated/legacy_test/{test_dist_fleet_a_sync_optimizer_auto_async.py => test_dist_fleet_a_sync_optimizer_auto_async_deprecated.py} (100%) rename test/deprecated/legacy_test/{test_dist_fleet_a_sync_optimizer_auto.py => test_dist_fleet_a_sync_optimizer_auto_deprecated.py} (100%) rename test/deprecated/legacy_test/{test_dist_fleet_a_sync_optimizer_auto_geo.py => test_dist_fleet_a_sync_optimizer_auto_geo_deprecated.py} (100%) rename test/deprecated/legacy_test/{test_dist_fleet_decay.py => test_dist_fleet_decay_deprecated.py} (100%) rename test/deprecated/legacy_test/{test_dist_fleet_heter_program.py => test_dist_fleet_heter_program_deprecated.py} (100%) rename test/deprecated/legacy_test/{test_dist_fleet_ps10.py => test_dist_fleet_ps10_deprecated.py} (100%) rename test/deprecated/legacy_test/{test_dist_fleet_ps13.py => test_dist_fleet_ps13_deprecated.py} (100%) rename test/deprecated/legacy_test/{test_dist_fleet_ps2.py => test_dist_fleet_ps2_deprecated.py} (100%) rename test/deprecated/legacy_test/{test_dist_fleet_ps3.py => test_dist_fleet_ps3_deprecated.py} (100%) rename test/deprecated/legacy_test/{test_dist_fleet_ps4.py => test_dist_fleet_ps4_deprecated.py} (100%) rename test/deprecated/legacy_test/{test_dist_fleet_ps5.py => test_dist_fleet_ps5_deprecated.py} (100%) rename test/deprecated/legacy_test/{test_dist_fleet_ps6.py => test_dist_fleet_ps6_deprecated.py} (100%) rename test/deprecated/legacy_test/{test_dist_fleet_ps7.py => test_dist_fleet_ps7_deprecated.py} (100%) rename test/deprecated/legacy_test/{test_dist_fleet_ps8.py => test_dist_fleet_ps8_deprecated.py} (100%) rename test/deprecated/legacy_test/{test_dist_fleet_ps9.py => test_dist_fleet_ps9_deprecated.py} (100%) rename test/deprecated/legacy_test/{test_dist_fleet_ps.py => test_dist_fleet_ps_deprecated.py} (100%) rename test/deprecated/legacy_test/{test_dist_sparse_tensor_load_sgd.py => test_dist_sparse_tensor_load_sgd_deprecated.py} (100%) rename test/deprecated/legacy_test/{test_eager_deletion_delete_vars.py => test_eager_deletion_delete_vars_deprecated.py} (100%) rename test/deprecated/legacy_test/{test_ema.py => test_ema_deprecated.py} (99%) rename test/deprecated/legacy_test/{test_ema_fleet.py => test_ema_fleet_deprecated.py} (100%) rename test/deprecated/legacy_test/{test_embedding_id_stop_gradient.py => test_embedding_id_stop_gradient_deprecated.py} (99%) rename test/deprecated/legacy_test/{test_entry_attr2.py => test_entry_attr2_deprecated.py} (100%) rename test/deprecated/legacy_test/{test_entry_attr.py => test_entry_attr_deprecated.py} (100%) rename test/deprecated/legacy_test/{test_error_clip.py => test_error_clip_deprecated.py} (100%) create mode 100644 test/deprecated/legacy_test/test_executor_and_use_program_cache_deprecated.py rename test/deprecated/legacy_test/{test_executor_check_feed.py => test_executor_check_feed_deprecated.py} (99%) create mode 100644 test/deprecated/legacy_test/test_executor_feed_non_tensor_deprecated.py rename test/deprecated/legacy_test/{test_fc_op.py => test_fc_op_deprecated.py} (66%) rename test/deprecated/legacy_test/{test_feed_data_check_shape_type.py => test_feed_data_check_shape_type_deprecated.py} (99%) rename test/deprecated/legacy_test/{test_fleet_base_2.py => test_fleet_base_2_deprecated.py} (100%) rename test/deprecated/legacy_test/{test_fleet_base_3.py => test_fleet_base_3_deprecated.py} (100%) create mode 100644 test/deprecated/legacy_test/test_fleet_base_deprecated.py rename test/deprecated/legacy_test/{test_fleet.py => test_fleet_deprecated.py} (99%) rename test/deprecated/legacy_test/{test_fleet_nocvm_1.py => test_fleet_nocvm_1_deprecated.py} (99%) rename test/deprecated/legacy_test/{test_fleet_unitaccessor.py => test_fleet_unitaccessor_deprecated.py} (99%) rename test/deprecated/legacy_test/{test_functional_conv2d.py => test_functional_conv2d_deprecated.py} (64%) rename test/deprecated/legacy_test/{test_functional_conv2d_transpose.py => test_functional_conv2d_transpose_deprecated.py} (99%) rename test/deprecated/legacy_test/{test_functional_conv3d.py => test_functional_conv3d_deprecated.py} (66%) create mode 100644 test/deprecated/legacy_test/test_functional_conv3d_transpose_deprecated.py rename test/deprecated/legacy_test/{test_fuse_bn_act_pass.py => test_fuse_bn_act_pass_deprecated.py} (99%) rename test/deprecated/legacy_test/{test_get_inputs_outputs_in_block.py => test_get_inputs_outputs_in_block_deprecated.py} (100%) rename test/deprecated/legacy_test/{test_gradient_clip.py => test_gradient_clip_deprecated.py} (100%) create mode 100644 test/deprecated/legacy_test/test_hsigmoid_op_deprecated.py create mode 100644 test/deprecated/legacy_test/test_image_classification_layer_deprecated.py create mode 100644 test/deprecated/legacy_test/test_imperative_double_grad_deprecated.py rename test/deprecated/legacy_test/{test_imperative_load_static_param.py => test_imperative_load_static_param_deprecated.py} (100%) rename test/deprecated/legacy_test/{test_imperative_lod_tensor_to_selected_rows.py => test_imperative_lod_tensor_to_selected_rows_deprecated.py} (100%) rename test/deprecated/legacy_test/{test_infer_no_need_buffer_slots.py => test_infer_no_need_buffer_slots_deprecated.py} (100%) rename test/deprecated/legacy_test/{test_inference_api.py => test_inference_api_deprecated.py} (81%) rename test/deprecated/legacy_test/{test_inference_model_io.py => test_inference_model_io_deprecated.py} (97%) create mode 100644 test/deprecated/legacy_test/test_initializer_deprecated.py create mode 100644 test/deprecated/legacy_test/test_instance_norm_op_deprecated.py create mode 100644 test/deprecated/legacy_test/test_layer_norm_op_deprecated.py create mode 100644 test/deprecated/legacy_test/test_math_op_patch_deprecated.py rename test/deprecated/legacy_test/{test_merged_momentum_op.py => test_merged_momentum_op_deprecated.py} (100%) create mode 100644 test/deprecated/legacy_test/test_momentum_op_deprecated.py rename test/deprecated/legacy_test/{test_name_scope.py => test_name_scope_deprecated.py} (98%) rename test/deprecated/legacy_test/{test_nce.py => test_nce_deprecated.py} (79%) rename test/deprecated/legacy_test/{test_optimizer.py => test_optimizer_deprecated.py} (88%) rename test/deprecated/legacy_test/{test_optimizer_in_control_flow.py => test_optimizer_in_control_flow_deprecated.py} (100%) create mode 100644 test/deprecated/legacy_test/test_prelu_op_deprecated.py rename test/deprecated/legacy_test/{test_program_code.py => test_program_code_deprecated.py} (100%) rename test/deprecated/legacy_test/{test_program.py => test_program_deprecated.py} (100%) rename test/deprecated/legacy_test/{test_program_prune_backward.py => test_program_prune_backward_deprecated.py} (100%) rename test/deprecated/legacy_test/{test_program_to_string.py => test_program_to_string_deprecated.py} (98%) rename test/deprecated/legacy_test/{test_prune.py => test_prune_deprecated.py} (99%) rename test/deprecated/legacy_test/{test_py_func_op.py => test_py_func_op_deprecated.py} (99%) create mode 100644 test/deprecated/legacy_test/test_random_seed_deprecated.py create mode 100644 test/deprecated/legacy_test/test_regularizer_api_deprecated.py rename test/deprecated/legacy_test/{test_regularizer.py => test_regularizer_deprecated.py} (70%) rename test/deprecated/legacy_test/{test_run_program_op.py => test_run_program_op_deprecated.py} (100%) rename test/deprecated/legacy_test/{test_select_input_output_op.py => test_select_input_output_op_deprecated.py} (100%) create mode 100644 test/deprecated/legacy_test/test_sgd_op_deprecated.py create mode 100644 test/deprecated/legacy_test/test_slice_op_deprecated.py rename test/deprecated/legacy_test/{test_static_pylayer_block.py => test_static_pylayer_block_deprecated.py} (100%) rename test/deprecated/legacy_test/{test_static_pylayer.py => test_static_pylayer_deprecated.py} (99%) rename test/deprecated/legacy_test/{test_switch.py => test_switch_deprecated.py} (99%) create mode 100644 test/deprecated/legacy_test/test_tensor_array_to_tensor_deprecated.py rename test/deprecated/legacy_test/{test_trainable.py => test_trainable_deprecated.py} (100%) rename test/deprecated/legacy_test/{test_truncated_gaussian_random_op.py => test_truncated_gaussian_random_op_deprecated.py} (100%) create mode 100644 test/deprecated/legacy_test/test_variable_deprecated.py rename test/deprecated/legacy_test/{test_weight_normalization.py => test_weight_normalization_deprecated.py} (99%) rename test/deprecated/prim/composite_ops/{test_composite_layer_norm.py => test_composite_layer_norm_deprecated.py} (100%) rename test/deprecated/prim/{test_comp_get_grad_op_desc_prim_enabled.py => test_comp_get_grad_op_desc_prim_enabled_deprecated.py} (100%) rename test/deprecated/prim/{test_comp_skip_op_set.py => test_comp_skip_op_set_deprecated.py} (100%) rename test/deprecated/quantization/{test_graph.py => test_graph_deprecated.py} (100%) rename test/deprecated/quantization/{test_moving_average_abs_max_scale_op.py => test_moving_average_abs_max_scale_op_deprecated.py} (100%) rename test/deprecated/quantization/{test_quant2_int8_mkldnn_pass.py => test_quant2_int8_mkldnn_pass_deprecated.py} (100%) rename test/deprecated/quantization/{test_quant_aware.py => test_quant_aware_deprecated.py} (100%) rename test/deprecated/quantization/{test_quantization_mkldnn_pass.py => test_quantization_mkldnn_pass_deprecated.py} (100%) rename test/deprecated/quantization/{test_quantization_pass.py => test_quantization_pass_deprecated.py} (100%) rename test/deprecated/quantization/{test_quantization_scale_pass.py => test_quantization_scale_pass_deprecated.py} (100%) rename test/deprecated/quantization/{test_user_defined_quantization.py => test_user_defined_quantization_deprecated.py} (100%) create mode 100644 test/deprecated/rnn/test_rnn_nets_deprecated.py create mode 100644 test/deprecated/rnn/test_rnn_nets_static_deprecated.py rename test/deprecated/standalone_executor/{test_standalone_dist_attr_run_time_set_get.py => test_standalone_dist_attr_run_time_set_get_deprecated.py} (100%) create mode 100644 test/deprecated/standalone_executor/test_standalone_executor_deprecated.py rename test/deprecated/standalone_executor/{test_standalone_executor_multi_micro_batch.py => test_standalone_executor_multi_micro_batch_deprecated.py} (100%) rename test/deprecated/standalone_executor/{test_standalone_executor_plan.py => test_standalone_executor_plan_deprecated.py} (100%) rename test/deprecated/standalone_executor/{test_standalone_op_priority.py => test_standalone_op_priority_deprecated.py} (100%) rename test/deprecated/standalone_executor/{test_standalone_sequentail_run.py => test_standalone_sequentail_run_deprecated.py} (100%) rename test/deprecated/tokenizer/{test_faster_tokenizer_op.py => test_faster_tokenizer_op_deprecated.py} (100%) rename test/{deprecated => }/legacy_test/test_adamax_api.py (66%) rename test/{deprecated => }/legacy_test/test_argsort_op.py (68%) create mode 100644 test/legacy_test/test_backward.py rename test/{deprecated => }/legacy_test/test_batch_norm_op.py (63%) create mode 100644 test/legacy_test/test_bilinear_tensor_product_op.py create mode 100644 test/legacy_test/test_conv3d_transpose_part2_op.py rename test/{deprecated => }/legacy_test/test_conv_nn_grad.py (58%) rename test/{deprecated => }/legacy_test/test_data_norm_op.py (91%) create mode 100644 test/legacy_test/test_deform_conv2d.py rename test/{deprecated => }/legacy_test/test_deformable_conv_op.py (85%) create mode 100644 test/legacy_test/test_device_guard.py rename test/{deprecated => }/legacy_test/test_executor_and_use_program_cache.py (66%) create mode 100644 test/legacy_test/test_fc_op.py create mode 100644 test/legacy_test/test_functional_conv2d.py create mode 100644 test/legacy_test/test_functional_conv3d.py rename test/{deprecated => }/legacy_test/test_hsigmoid_op.py (89%) create mode 100644 test/legacy_test/test_inference_api.py create mode 100644 test/legacy_test/test_inference_model_io.py rename test/{deprecated => }/legacy_test/test_layer_norm_op.py (73%) create mode 100644 test/legacy_test/test_nce.py create mode 100644 test/legacy_test/test_optimizer.py rename test/{deprecated => }/legacy_test/test_prelu_op.py (88%) rename test/{deprecated => }/legacy_test/test_random_seed.py (87%) create mode 100644 test/legacy_test/test_regularizer.py rename test/{deprecated => }/legacy_test/test_regularizer_api.py (77%) rename test/{deprecated => }/legacy_test/test_sgd_op.py (60%) diff --git a/test/contrib/CMakeLists.txt b/test/contrib/CMakeLists.txt index e723b8abaf396c..02219f03129b8c 100644 --- a/test/contrib/CMakeLists.txt +++ b/test/contrib/CMakeLists.txt @@ -20,3 +20,4 @@ py_test_modules( FLAGS_conv_workspace_size_limit=1000) set_tests_properties(test_multi_precision_fp16_train PROPERTIES TIMEOUT 120) +set_tests_properties(test_image_classification_fp16 PROPERTIES TIMEOUT 120) diff --git a/test/contrib/test_bf16_utils.py b/test/contrib/test_bf16_utils.py new file mode 100644 index 00000000000000..ce542e9603dad5 --- /dev/null +++ b/test/contrib/test_bf16_utils.py @@ -0,0 +1,111 @@ +# 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 copy +import unittest + +import paddle +from paddle.static import amp + +paddle.enable_static() + + +class AMPTest(unittest.TestCase): + def setUp(self): + self.bf16_list = copy.copy(amp.bf16.amp_lists.bf16_list) + self.fp32_list = copy.copy(amp.bf16.amp_lists.fp32_list) + self.gray_list = copy.copy(amp.bf16.amp_lists.gray_list) + self.amp_lists_ = None + + def tearDown(self): + self.assertEqual(self.amp_lists_.bf16_list, self.bf16_list) + self.assertEqual(self.amp_lists_.fp32_list, self.fp32_list) + self.assertEqual(self.amp_lists_.gray_list, self.gray_list) + + def test_amp_lists(self): + self.amp_lists_ = amp.bf16.AutoMixedPrecisionListsBF16() + + def test_amp_lists_1(self): + # 1. w={'exp}, b=None + self.bf16_list.add('exp') + self.fp32_list.remove('exp') + + self.amp_lists_ = amp.bf16.AutoMixedPrecisionListsBF16({'exp'}) + + def test_amp_lists_2(self): + # 2. w={'tanh'}, b=None + self.fp32_list.remove('tan') + self.bf16_list.add('tan') + + self.amp_lists_ = amp.bf16.AutoMixedPrecisionListsBF16({'tan'}) + + def test_amp_lists_3(self): + # 3. w={'lstm'}, b=None + self.bf16_list.add('lstm') + + self.amp_lists_ = amp.bf16.AutoMixedPrecisionListsBF16({'lstm'}) + + def test_amp_lists_4(self): + # 4. w=None, b={'matmul_v2'} + self.bf16_list.remove('matmul_v2') + self.fp32_list.add('matmul_v2') + + self.amp_lists_ = amp.bf16.AutoMixedPrecisionListsBF16( + custom_fp32_list={'matmul_v2'} + ) + + def test_amp_lists_5(self): + # 5. w=None, b={'matmul_v2'} + self.fp32_list.add('matmul_v2') + self.bf16_list.remove('matmul_v2') + + self.amp_lists_ = amp.bf16.AutoMixedPrecisionListsBF16( + custom_fp32_list={'matmul_v2'} + ) + + def test_amp_lists_6(self): + # 6. w=None, b={'lstm'} + self.fp32_list.add('lstm') + + self.amp_lists_ = amp.bf16.AutoMixedPrecisionListsBF16( + custom_fp32_list={'lstm'} + ) + + def test_amp_lists_7(self): + self.fp32_list.add('reshape2') + self.gray_list.remove('reshape2') + + self.amp_lists_ = amp.bf16.AutoMixedPrecisionListsBF16( + custom_fp32_list={'reshape2'} + ) + + def test_amp_list_8(self): + self.bf16_list.add('reshape2') + self.gray_list.remove('reshape2') + + self.amp_lists_ = amp.bf16.AutoMixedPrecisionListsBF16( + custom_bf16_list={'reshape2'} + ) + + +class AMPTest2(unittest.TestCase): + def test_amp_lists_(self): + # 7. w={'lstm'} b={'lstm'} + # raise ValueError + self.assertRaises( + ValueError, amp.bf16.AutoMixedPrecisionListsBF16, {'lstm'}, {'lstm'} + ) + + +if __name__ == '__main__': + unittest.main() diff --git a/test/contrib/test_image_classification_fp16.py b/test/contrib/test_image_classification_fp16.py new file mode 100644 index 00000000000000..155fd5b2279135 --- /dev/null +++ b/test/contrib/test_image_classification_fp16.py @@ -0,0 +1,198 @@ +# 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. + +import contextlib +import copy +import sys +import tempfile +import unittest + +# TODO: remove sys.path.append +sys.path.append("../../legacy_test") + +import paddle +from paddle import base + +paddle.enable_static() + + +class TestImageClassification(unittest.TestCase): + def setUp(self): + self.temp_dir = tempfile.TemporaryDirectory() + + def tearDown(self): + self.temp_dir.cleanup() + + def test_amp_lists(self): + white_list = ( + copy.copy(paddle.static.amp.fp16_lists.white_list) + | paddle.static.amp.fp16_lists._only_supported_fp16_list + ) + black_list = copy.copy( + paddle.static.amp.fp16_lists.black_list + | paddle.static.amp.fp16_lists._extra_black_list + ) + gray_list = copy.copy(paddle.static.amp.fp16_lists.gray_list) + + amp_lists = paddle.static.amp.AutoMixedPrecisionLists() + self.assertEqual(amp_lists.white_list, white_list) + self.assertEqual(amp_lists.black_list, black_list) + self.assertEqual(amp_lists.gray_list, gray_list) + + def test_amp_lists_1(self): + white_list = ( + copy.copy(paddle.static.amp.fp16_lists.white_list) + | paddle.static.amp.fp16_lists._only_supported_fp16_list + ) + black_list = copy.copy( + paddle.static.amp.fp16_lists.black_list + | paddle.static.amp.fp16_lists._extra_black_list + ) + gray_list = copy.copy(paddle.static.amp.fp16_lists.gray_list) + + # 1. w={'exp}, b=None + white_list.add('exp') + black_list.remove('exp') + + amp_lists = paddle.static.amp.AutoMixedPrecisionLists({'exp'}) + self.assertEqual(amp_lists.white_list, white_list) + self.assertEqual(amp_lists.black_list, black_list) + self.assertEqual(amp_lists.gray_list, gray_list) + + def test_amp_lists_2(self): + white_list = ( + copy.copy(paddle.static.amp.fp16_lists.white_list) + | paddle.static.amp.fp16_lists._only_supported_fp16_list + ) + black_list = copy.copy( + paddle.static.amp.fp16_lists.black_list + | paddle.static.amp.fp16_lists._extra_black_list + ) + gray_list = copy.copy(paddle.static.amp.fp16_lists.gray_list) + + # 2. w={'tanh'}, b=None + white_list.add('tanh') + gray_list.remove('tanh') + + amp_lists = paddle.static.amp.AutoMixedPrecisionLists({'tanh'}) + self.assertEqual(amp_lists.white_list, white_list) + self.assertEqual(amp_lists.black_list, black_list) + self.assertEqual(amp_lists.gray_list, gray_list) + + def test_amp_lists_3(self): + white_list = ( + copy.copy(paddle.static.amp.fp16_lists.white_list) + | paddle.static.amp.fp16_lists._only_supported_fp16_list + ) + black_list = copy.copy( + paddle.static.amp.fp16_lists.black_list + | paddle.static.amp.fp16_lists._extra_black_list + ) + gray_list = copy.copy(paddle.static.amp.fp16_lists.gray_list) + + # 3. w={'lstm'}, b=None + white_list.add('lstm') + + amp_lists = paddle.static.amp.AutoMixedPrecisionLists({'lstm'}) + self.assertEqual(amp_lists.white_list, white_list) + self.assertEqual(amp_lists.black_list, black_list) + self.assertEqual(amp_lists.gray_list, gray_list) + + def test_amp_lists_4(self): + white_list = ( + copy.copy(paddle.static.amp.fp16_lists.white_list) + | paddle.static.amp.fp16_lists._only_supported_fp16_list + ) + black_list = copy.copy( + paddle.static.amp.fp16_lists.black_list + | paddle.static.amp.fp16_lists._extra_black_list + ) + gray_list = copy.copy(paddle.static.amp.fp16_lists.gray_list) + + # 4. w=None, b={'conv2d'} + white_list.remove('conv2d') + black_list.add('conv2d') + + amp_lists = paddle.static.amp.AutoMixedPrecisionLists( + custom_black_list={'conv2d'} + ) + self.assertEqual(amp_lists.white_list, white_list) + self.assertEqual(amp_lists.black_list, black_list) + self.assertEqual(amp_lists.gray_list, gray_list) + + def test_amp_lists_5(self): + white_list = ( + copy.copy(paddle.static.amp.fp16_lists.white_list) + | paddle.static.amp.fp16_lists._only_supported_fp16_list + ) + black_list = copy.copy( + paddle.static.amp.fp16_lists.black_list + | paddle.static.amp.fp16_lists._extra_black_list + ) + gray_list = copy.copy(paddle.static.amp.fp16_lists.gray_list) + + # 5. w=None, b={'tanh'} + black_list.add('tanh') + gray_list.remove('tanh') + + amp_lists = paddle.static.amp.AutoMixedPrecisionLists( + custom_black_list={'tanh'} + ) + self.assertEqual(amp_lists.white_list, white_list) + self.assertEqual(amp_lists.black_list, black_list) + self.assertEqual(amp_lists.gray_list, gray_list) + + def test_amp_lists_6(self): + white_list = ( + copy.copy(paddle.static.amp.fp16_lists.white_list) + | paddle.static.amp.fp16_lists._only_supported_fp16_list + ) + black_list = copy.copy( + paddle.static.amp.fp16_lists.black_list + | paddle.static.amp.fp16_lists._extra_black_list + ) + gray_list = copy.copy(paddle.static.amp.fp16_lists.gray_list) + + # 6. w=None, b={'lstm'} + black_list.add('lstm') + + amp_lists = paddle.static.amp.AutoMixedPrecisionLists( + custom_black_list={'lstm'} + ) + self.assertEqual(amp_lists.white_list, white_list) + self.assertEqual(amp_lists.black_list, black_list) + self.assertEqual(amp_lists.gray_list, gray_list) + + def test_amp_lists_7(self): + # 7. w={'lstm'} b={'lstm'} + # raise ValueError + self.assertRaises( + ValueError, + paddle.static.amp.AutoMixedPrecisionLists, + {'lstm'}, + {'lstm'}, + ) + + @contextlib.contextmanager + def scope_prog_guard(self): + prog = base.Program() + startup_prog = base.Program() + scope = base.core.Scope() + with base.scope_guard(scope): + with base.program_guard(prog, startup_prog): + yield + + +if __name__ == '__main__': + unittest.main() diff --git a/test/deprecated/asp/CMakeLists.txt b/test/deprecated/asp/CMakeLists.txt index 24b7364d5ba686..886d7908d0e0a0 100644 --- a/test/deprecated/asp/CMakeLists.txt +++ b/test/deprecated/asp/CMakeLists.txt @@ -9,6 +9,6 @@ foreach(TEST_OP ${TEST_OPS}) endforeach() set_tests_properties(test_asp_pruning_dynamic PROPERTIES TIMEOUT 30) -set_tests_properties(test_asp_pruning_static PROPERTIES TIMEOUT 30) +set_tests_properties(test_asp_pruning_static_deprecated PROPERTIES TIMEOUT 30) set_tests_properties(test_asp_optimize_dynamic PROPERTIES TIMEOUT 30) -set_tests_properties(test_asp_optimize_static PROPERTIES TIMEOUT 30) +set_tests_properties(test_asp_optimize_static_deprecated PROPERTIES TIMEOUT 30) diff --git a/test/deprecated/asp/test_asp_customized_pruning.py b/test/deprecated/asp/test_asp_customized_pruning.py index f17acd61de42a8..44bb65682fef00 100644 --- a/test/deprecated/asp/test_asp_customized_pruning.py +++ b/test/deprecated/asp/test_asp_customized_pruning.py @@ -18,8 +18,6 @@ import numpy as np import paddle -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, @@ -184,160 +182,5 @@ def test_training_pruning(self): self.assertEqual(supported_layer_count, self.supported_layer_count_ref) -class TestASPStaticCustomizedPruneFunc(unittest.TestCase): - def setUp(self): - paddle.enable_static() - - self.main_program = base.Program() - self.startup_program = base.Program() - - self.customer_prefix = "customer_layer" - - def build_model(): - img = paddle.static.data( - name='img', shape=[None, 3, 32, 32], dtype='float32' - ) - label = paddle.static.data( - name='label', shape=[None, 1], dtype='int64' - ) - hidden = paddle.static.nn.conv2d( - input=img, num_filters=4, filter_size=3, padding=2, act="relu" - ) - hidden = paddle.static.nn.fc( - x=hidden, size=32, activation='relu', name=self.customer_prefix - ) - hidden = paddle.static.nn.fc( - x=hidden, size=32, activation='relu', name=self.customer_prefix - ) - hidden = paddle.static.nn.fc(x=hidden, size=32, activation='relu') - prediction = paddle.static.nn.fc( - x=hidden, size=10, activation='softmax' - ) - return img, label, prediction - - 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 = base.Executor(self.place) - - sparsity.add_supported_layer(self.customer_prefix, my_own_pruning) - - def test_inference_pruning(self): - self.exe.run(self.startup_program) - - sparsity.prune_model( - self.main_program, mask_algo="mask_1d", with_mask=False - ) - - supported_layer_count = 0 - for param in self.main_program.global_block().all_parameters(): - mat = np.array( - base.global_scope().find_var(param.name).get_tensor() - ) - if sparsity.asp.ASPHelper._is_supported_layer( - self.main_program, param.name - ): - supported_layer_count += 1 - if self.customer_prefix in param.name: - self.assertLessEqual( - np.sum(mat.flatten() - static_tensor.flatten()), 1e-4 - ) - else: - if (len(param.shape) == 4 and param.shape[1] < 4) or ( - len(param.shape) == 2 and param.shape[0] < 4 - ): - self.assertFalse( - paddle.incubate.asp.check_sparsity(mat.T, n=2, m=4) - ) - else: - self.assertTrue( - sparsity.check_sparsity( - mat.T, - func_name=sparsity.CheckMethod.CHECK_1D, - n=2, - m=4, - ) - ) - self.assertEqual(supported_layer_count, self.supported_layer_count_ref) - - def test_training_pruning(self): - with base.program_guard(self.main_program, self.startup_program): - loss = paddle.mean( - paddle.nn.functional.cross_entropy( - input=self.predict, - label=self.label, - reduction='none', - use_softmax=False, - ) - ) - optimizer = sparsity.decorate( - paddle.optimizer.SGD(learning_rate=0.01) - ) - optimizer.minimize(loss, self.startup_program) - - self.exe.run(self.startup_program) - - sparsity.prune_model( - self.main_program, mask_algo="mask_1d", with_mask=True - ) - - supported_layer_count = 0 - for param in self.main_program.global_block().all_parameters(): - mat = np.array( - 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( - base.global_scope() - .find_var(sparsity.asp.ASPHelper._get_mask_name(param.name)) - .get_tensor() - ) - supported_layer_count += 1 - if self.customer_prefix in param.name: - self.assertLessEqual( - np.sum(mat.flatten() - static_tensor.flatten()), 1e-4 - ) - self.assertLessEqual( - np.sum( - mat_mask.flatten() - static_tensor_mask.flatten() - ), - 1e-4, - ) - else: - if (len(param.shape) == 4 and param.shape[1] < 4) or ( - len(param.shape) == 2 and param.shape[0] < 4 - ): - self.assertFalse( - sparsity.check_sparsity(mat.T, n=2, m=4) - ) - self.assertFalse( - sparsity.check_sparsity(mat_mask.T, n=2, m=4) - ) - else: - self.assertTrue( - sparsity.check_sparsity( - mat.T, - func_name=sparsity.CheckMethod.CHECK_1D, - n=2, - m=4, - ) - ) - self.assertTrue( - sparsity.check_sparsity( - mat_mask.T, - func_name=sparsity.CheckMethod.CHECK_1D, - n=2, - m=4, - ) - ) - self.assertEqual(supported_layer_count, self.supported_layer_count_ref) - - if __name__ == '__main__': unittest.main() diff --git a/test/deprecated/asp/test_asp_customized_pruning_deprecated.py b/test/deprecated/asp/test_asp_customized_pruning_deprecated.py new file mode 100644 index 00000000000000..c088c1c827f5ce --- /dev/null +++ b/test/deprecated/asp/test_asp_customized_pruning_deprecated.py @@ -0,0 +1,205 @@ +# Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved. +# Copyright (c) 2022 NVIDIA Corporation. 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 base +from paddle.base import core +from paddle.incubate import asp as sparsity +from paddle.nn.layer.layers import Layer + + +class MyOwnLayer(Layer): + def __init__(self): + super().__init__() + + def forward(self, x): + return x + + +static_tensor = None +static_tensor_mask = None + + +def my_own_pruning(tensor, m, n, mask_algo, param_name): + global static_tensor + global static_tensor_mask + if static_tensor is None: + static_tensor = np.random.rand(*tensor.shape).astype(np.float32) + if static_tensor_mask is None: + static_tensor_mask = np.random.rand(*tensor.shape).astype(np.float32) + return static_tensor, static_tensor_mask + + +class TestASPStaticCustomizedPruneFunc(unittest.TestCase): + def setUp(self): + paddle.enable_static() + + self.main_program = base.Program() + self.startup_program = base.Program() + + self.customer_prefix = "customer_layer" + + def build_model(): + img = paddle.static.data( + name='img', shape=[None, 3, 32, 32], dtype='float32' + ) + label = paddle.static.data( + name='label', shape=[None, 1], dtype='int64' + ) + hidden = paddle.static.nn.conv2d( + input=img, num_filters=4, filter_size=3, padding=2, act="relu" + ) + hidden = paddle.static.nn.fc( + x=hidden, size=32, activation='relu', name=self.customer_prefix + ) + hidden = paddle.static.nn.fc( + x=hidden, size=32, activation='relu', name=self.customer_prefix + ) + hidden = paddle.static.nn.fc(x=hidden, size=32, activation='relu') + prediction = paddle.static.nn.fc( + x=hidden, size=10, activation='softmax' + ) + return img, label, prediction + + 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 = base.Executor(self.place) + + sparsity.add_supported_layer(self.customer_prefix, my_own_pruning) + + def test_inference_pruning(self): + self.exe.run(self.startup_program) + + sparsity.prune_model( + self.main_program, mask_algo="mask_1d", with_mask=False + ) + + supported_layer_count = 0 + for param in self.main_program.global_block().all_parameters(): + mat = np.array( + base.global_scope().find_var(param.name).get_tensor() + ) + if sparsity.asp.ASPHelper._is_supported_layer( + self.main_program, param.name + ): + supported_layer_count += 1 + if self.customer_prefix in param.name: + self.assertLessEqual( + np.sum(mat.flatten() - static_tensor.flatten()), 1e-4 + ) + else: + if (len(param.shape) == 4 and param.shape[1] < 4) or ( + len(param.shape) == 2 and param.shape[0] < 4 + ): + self.assertFalse( + paddle.incubate.asp.check_sparsity(mat.T, n=2, m=4) + ) + else: + self.assertTrue( + sparsity.check_sparsity( + mat.T, + func_name=sparsity.CheckMethod.CHECK_1D, + n=2, + m=4, + ) + ) + self.assertEqual(supported_layer_count, self.supported_layer_count_ref) + + def test_training_pruning(self): + with base.program_guard(self.main_program, self.startup_program): + loss = paddle.mean( + paddle.nn.functional.cross_entropy( + input=self.predict, + label=self.label, + reduction='none', + use_softmax=False, + ) + ) + optimizer = sparsity.decorate( + paddle.optimizer.SGD(learning_rate=0.01) + ) + optimizer.minimize(loss, self.startup_program) + + self.exe.run(self.startup_program) + + sparsity.prune_model( + self.main_program, mask_algo="mask_1d", with_mask=True + ) + + supported_layer_count = 0 + for param in self.main_program.global_block().all_parameters(): + mat = np.array( + 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( + base.global_scope() + .find_var(sparsity.asp.ASPHelper._get_mask_name(param.name)) + .get_tensor() + ) + supported_layer_count += 1 + if self.customer_prefix in param.name: + self.assertLessEqual( + np.sum(mat.flatten() - static_tensor.flatten()), 1e-4 + ) + self.assertLessEqual( + np.sum( + mat_mask.flatten() - static_tensor_mask.flatten() + ), + 1e-4, + ) + else: + if (len(param.shape) == 4 and param.shape[1] < 4) or ( + len(param.shape) == 2 and param.shape[0] < 4 + ): + self.assertFalse( + sparsity.check_sparsity(mat.T, n=2, m=4) + ) + self.assertFalse( + sparsity.check_sparsity(mat_mask.T, n=2, m=4) + ) + else: + self.assertTrue( + sparsity.check_sparsity( + mat.T, + func_name=sparsity.CheckMethod.CHECK_1D, + n=2, + m=4, + ) + ) + self.assertTrue( + sparsity.check_sparsity( + mat_mask.T, + func_name=sparsity.CheckMethod.CHECK_1D, + n=2, + m=4, + ) + ) + self.assertEqual(supported_layer_count, self.supported_layer_count_ref) + + +if __name__ == '__main__': + unittest.main() diff --git a/test/deprecated/asp/test_asp_optimize_static.py b/test/deprecated/asp/test_asp_optimize_static_deprecated.py similarity index 100% rename from test/deprecated/asp/test_asp_optimize_static.py rename to test/deprecated/asp/test_asp_optimize_static_deprecated.py diff --git a/test/deprecated/asp/test_asp_pruning_static.py b/test/deprecated/asp/test_asp_pruning_static_deprecated.py similarity index 100% rename from test/deprecated/asp/test_asp_pruning_static.py rename to test/deprecated/asp/test_asp_pruning_static_deprecated.py diff --git a/test/deprecated/asp/test_asp_save_load.py b/test/deprecated/asp/test_asp_save_load.py index f9966c321b37e4..83379f2f5ae3cb 100644 --- a/test/deprecated/asp/test_asp_save_load.py +++ b/test/deprecated/asp/test_asp_save_load.py @@ -18,7 +18,6 @@ import numpy as np import paddle -from paddle import base from paddle.base import core from paddle.incubate.asp import ASPHelper @@ -120,90 +119,5 @@ def test_save_and_load(self): ) -class TestASPStaticOptimize(unittest.TestCase): - def setUp(self): - paddle.enable_static() - - self.main_program = base.Program() - self.startup_program = base.Program() - - def build_model(): - img = paddle.static.data( - name='img', shape=[None, 3, 32, 32], dtype='float32' - ) - label = paddle.static.data( - name='label', shape=[None, 1], dtype='int64' - ) - hidden = paddle.static.nn.conv2d( - input=img, num_filters=4, filter_size=3, padding=2, act="relu" - ) - hidden = paddle.static.nn.fc(x=hidden, size=32, activation='relu') - prediction = paddle.static.nn.fc( - x=hidden, size=10, activation='softmax' - ) - return img, label, prediction - - 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( - input=predict, - label=self.label, - reduction='none', - use_softmax=False, - ) - ) - self.optimizer = paddle.optimizer.SGD(learning_rate=0.01) - self.optimizer = paddle.incubate.asp.decorate(self.optimizer) - self.optimizer.minimize(self.loss, self.startup_program) - - self.place = paddle.CPUPlace() - if core.is_compiled_with_cuda(): - self.place = paddle.CUDAPlace(0) - self.exe = base.Executor(self.place) - self.exe.run(self.startup_program) - - paddle.incubate.asp.prune_model(self.main_program) - - def test_save_and_load(self): - path = "/tmp/paddle_asp_save_st/" - param_path = path + "asp.pdparams" - model_path = path + "asp.pdmodel" - - paddle.save(self.main_program.state_dict(), param_path) - paddle.save(self.main_program, model_path) - - prog = paddle.load(model_path) - - state_dict = paddle.load(param_path) - prog.set_state_dict(state_dict) - - feeder = base.DataFeeder( - feed_list=[self.img, self.label], place=self.place - ) - - data = ( - np.random.randn(64, 3, 32, 32), - np.random.randint(10, size=(64, 1)), - ) - self.exe.run(prog, feed=feeder.feed([data])) - - for param in prog.global_block().all_parameters(): - if ASPHelper._is_supported_layer(prog, param.name): - mat = np.array( - 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 - ): - self.assertFalse( - paddle.incubate.asp.check_sparsity(mat.T, n=2, m=4) - ) - else: - self.assertTrue( - paddle.incubate.asp.check_sparsity(mat.T, n=2, m=4) - ) - - if __name__ == '__main__': unittest.main() diff --git a/test/deprecated/asp/test_asp_save_load_deprecated.py b/test/deprecated/asp/test_asp_save_load_deprecated.py new file mode 100644 index 00000000000000..28386b1d2df547 --- /dev/null +++ b/test/deprecated/asp/test_asp_save_load_deprecated.py @@ -0,0 +1,131 @@ +# Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved. +# Copyright (c) 2022 NVIDIA Corporation. 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 base +from paddle.base import core +from paddle.incubate.asp import ASPHelper + + +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 + + +class TestASPStaticOptimize(unittest.TestCase): + def setUp(self): + paddle.enable_static() + + self.main_program = base.Program() + self.startup_program = base.Program() + + def build_model(): + img = paddle.static.data( + name='img', shape=[None, 3, 32, 32], dtype='float32' + ) + label = paddle.static.data( + name='label', shape=[None, 1], dtype='int64' + ) + hidden = paddle.static.nn.conv2d( + input=img, num_filters=4, filter_size=3, padding=2, act="relu" + ) + hidden = paddle.static.nn.fc(x=hidden, size=32, activation='relu') + prediction = paddle.static.nn.fc( + x=hidden, size=10, activation='softmax' + ) + return img, label, prediction + + 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( + input=predict, + label=self.label, + reduction='none', + use_softmax=False, + ) + ) + self.optimizer = paddle.optimizer.SGD(learning_rate=0.01) + self.optimizer = paddle.incubate.asp.decorate(self.optimizer) + self.optimizer.minimize(self.loss, self.startup_program) + + self.place = paddle.CPUPlace() + if core.is_compiled_with_cuda(): + self.place = paddle.CUDAPlace(0) + self.exe = base.Executor(self.place) + self.exe.run(self.startup_program) + + paddle.incubate.asp.prune_model(self.main_program) + + def test_save_and_load(self): + path = "/tmp/paddle_asp_save_st/" + param_path = path + "asp.pdparams" + model_path = path + "asp.pdmodel" + + paddle.save(self.main_program.state_dict(), param_path) + paddle.save(self.main_program, model_path) + + prog = paddle.load(model_path) + + state_dict = paddle.load(param_path) + prog.set_state_dict(state_dict) + + feeder = base.DataFeeder( + feed_list=[self.img, self.label], place=self.place + ) + + data = ( + np.random.randn(64, 3, 32, 32), + np.random.randint(10, size=(64, 1)), + ) + self.exe.run(prog, feed=feeder.feed([data])) + + for param in prog.global_block().all_parameters(): + if ASPHelper._is_supported_layer(prog, param.name): + mat = np.array( + 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 + ): + self.assertFalse( + paddle.incubate.asp.check_sparsity(mat.T, n=2, m=4) + ) + else: + self.assertTrue( + paddle.incubate.asp.check_sparsity(mat.T, n=2, m=4) + ) + + +if __name__ == '__main__': + unittest.main() diff --git a/test/deprecated/collective/fleet/CMakeLists.txt b/test/deprecated/collective/fleet/CMakeLists.txt index c3d8d0e48e9dc7..304f1858479172 100644 --- a/test/deprecated/collective/fleet/CMakeLists.txt +++ b/test/deprecated/collective/fleet/CMakeLists.txt @@ -13,8 +13,8 @@ endif() if(LOCAL_ALL_ARCH AND (LINUX OR WIN32)) py_test_modules( - test_fleet_fp16_allreduce_meta_optimizer MODULES - test_fleet_fp16_allreduce_meta_optimizer ENVS + test_fleet_fp16_allreduce_meta_optimizer_deprecated MODULES + test_fleet_fp16_allreduce_meta_optimizer_deprecated ENVS "http_proxy=;https_proxy=;PYTHONPATH=../..:${PADDLE_BINARY_DIR}/python") endif() @@ -38,6 +38,7 @@ endif() if(LOCAL_ALL_ARCH AND (LINUX OR WIN32)) py_test_modules( - test_fleet_meta_optimizer_base MODULES test_fleet_meta_optimizer_base ENVS + test_fleet_meta_optimizer_base_deprecated MODULES + test_fleet_meta_optimizer_base_deprecated ENVS "http_proxy=;https_proxy=;PYTHONPATH=../..:${PADDLE_BINARY_DIR}/python") endif() diff --git a/test/deprecated/collective/fleet/test_fleet_fp16_allreduce_meta_optimizer.py b/test/deprecated/collective/fleet/test_fleet_fp16_allreduce_meta_optimizer_deprecated.py similarity index 100% rename from test/deprecated/collective/fleet/test_fleet_fp16_allreduce_meta_optimizer.py rename to test/deprecated/collective/fleet/test_fleet_fp16_allreduce_meta_optimizer_deprecated.py diff --git a/test/deprecated/collective/fleet/test_fleet_meta_optimizer_base.py b/test/deprecated/collective/fleet/test_fleet_meta_optimizer_base_deprecated.py similarity index 99% rename from test/deprecated/collective/fleet/test_fleet_meta_optimizer_base.py rename to test/deprecated/collective/fleet/test_fleet_meta_optimizer_base_deprecated.py index 8878fdc172e2f1..2da076437aede8 100755 --- a/test/deprecated/collective/fleet/test_fleet_meta_optimizer_base.py +++ b/test/deprecated/collective/fleet/test_fleet_meta_optimizer_base_deprecated.py @@ -22,6 +22,8 @@ MetaOptimizerBase, ) +paddle.enable_static() + class TestFleetMetaOptimizerBase(unittest.TestCase): def net(main_prog, startup_prog): diff --git a/test/deprecated/contrib/CMakeLists.txt b/test/deprecated/contrib/CMakeLists.txt index a8ed413e6ce9e2..fb82eaa2b6817d 100644 --- a/test/deprecated/contrib/CMakeLists.txt +++ b/test/deprecated/contrib/CMakeLists.txt @@ -8,4 +8,5 @@ foreach(src ${TEST_OPS}) py_test(${src} SRCS ${src}.py) endforeach() -set_tests_properties(test_image_classification_fp16 PROPERTIES TIMEOUT 120) +set_tests_properties(test_image_classification_fp16_deprecated + PROPERTIES TIMEOUT 120) diff --git a/test/deprecated/contrib/test_bf16_utils.py b/test/deprecated/contrib/test_bf16_utils_deprecated.py similarity index 57% rename from test/deprecated/contrib/test_bf16_utils.py rename to test/deprecated/contrib/test_bf16_utils_deprecated.py index 8bc3cf43b87480..54f3ff73e00991 100644 --- a/test/deprecated/contrib/test_bf16_utils.py +++ b/test/deprecated/contrib/test_bf16_utils_deprecated.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 copy import unittest import paddle @@ -22,92 +21,7 @@ paddle.enable_static() -class AMPTest(unittest.TestCase): - def setUp(self): - self.bf16_list = copy.copy(amp.bf16.amp_lists.bf16_list) - self.fp32_list = copy.copy(amp.bf16.amp_lists.fp32_list) - self.gray_list = copy.copy(amp.bf16.amp_lists.gray_list) - self.amp_lists_ = None - - def tearDown(self): - self.assertEqual(self.amp_lists_.bf16_list, self.bf16_list) - self.assertEqual(self.amp_lists_.fp32_list, self.fp32_list) - self.assertEqual(self.amp_lists_.gray_list, self.gray_list) - - def test_amp_lists(self): - self.amp_lists_ = amp.bf16.AutoMixedPrecisionListsBF16() - - def test_amp_lists_1(self): - # 1. w={'exp}, b=None - self.bf16_list.add('exp') - self.fp32_list.remove('exp') - - self.amp_lists_ = amp.bf16.AutoMixedPrecisionListsBF16({'exp'}) - - def test_amp_lists_2(self): - # 2. w={'tanh'}, b=None - self.fp32_list.remove('tan') - self.bf16_list.add('tan') - - self.amp_lists_ = amp.bf16.AutoMixedPrecisionListsBF16({'tan'}) - - def test_amp_lists_3(self): - # 3. w={'lstm'}, b=None - self.bf16_list.add('lstm') - - self.amp_lists_ = amp.bf16.AutoMixedPrecisionListsBF16({'lstm'}) - - def test_amp_lists_4(self): - # 4. w=None, b={'matmul_v2'} - self.bf16_list.remove('matmul_v2') - self.fp32_list.add('matmul_v2') - - self.amp_lists_ = amp.bf16.AutoMixedPrecisionListsBF16( - custom_fp32_list={'matmul_v2'} - ) - - def test_amp_lists_5(self): - # 5. w=None, b={'matmul_v2'} - self.fp32_list.add('matmul_v2') - self.bf16_list.remove('matmul_v2') - - self.amp_lists_ = amp.bf16.AutoMixedPrecisionListsBF16( - custom_fp32_list={'matmul_v2'} - ) - - def test_amp_lists_6(self): - # 6. w=None, b={'lstm'} - self.fp32_list.add('lstm') - - self.amp_lists_ = amp.bf16.AutoMixedPrecisionListsBF16( - custom_fp32_list={'lstm'} - ) - - def test_amp_lists_7(self): - self.fp32_list.add('reshape2') - self.gray_list.remove('reshape2') - - self.amp_lists_ = amp.bf16.AutoMixedPrecisionListsBF16( - custom_fp32_list={'reshape2'} - ) - - def test_amp_list_8(self): - self.bf16_list.add('reshape2') - self.gray_list.remove('reshape2') - - self.amp_lists_ = amp.bf16.AutoMixedPrecisionListsBF16( - custom_bf16_list={'reshape2'} - ) - - class AMPTest2(unittest.TestCase): - def test_amp_lists_(self): - # 7. w={'lstm'} b={'lstm'} - # raise ValueError - self.assertRaises( - ValueError, amp.bf16.AutoMixedPrecisionListsBF16, {'lstm'}, {'lstm'} - ) - def test_find_op_index(self): block = base.default_main_program().global_block() op_desc = core.OpDesc() diff --git a/test/deprecated/contrib/test_image_classification_fp16.py b/test/deprecated/contrib/test_image_classification_fp16_deprecated.py similarity index 70% rename from test/deprecated/contrib/test_image_classification_fp16.py rename to test/deprecated/contrib/test_image_classification_fp16_deprecated.py index c3cfa834a4ed24..a4f1bbe3d34190 100644 --- a/test/deprecated/contrib/test_image_classification_fp16.py +++ b/test/deprecated/contrib/test_image_classification_fp16_deprecated.py @@ -13,7 +13,6 @@ # limitations under the License. import contextlib -import copy import math import os import sys @@ -306,156 +305,6 @@ def main(self, net_type, use_cuda, is_local=True): train(net_type, use_cuda, save_dirname, is_local) # infer(use_cuda, save_dirname) - def test_amp_lists(self): - white_list = ( - copy.copy(paddle.static.amp.fp16_lists.white_list) - | paddle.static.amp.fp16_lists._only_supported_fp16_list - ) - black_list = copy.copy( - paddle.static.amp.fp16_lists.black_list - | paddle.static.amp.fp16_lists._extra_black_list - ) - gray_list = copy.copy(paddle.static.amp.fp16_lists.gray_list) - - amp_lists = paddle.static.amp.AutoMixedPrecisionLists() - self.assertEqual(amp_lists.white_list, white_list) - self.assertEqual(amp_lists.black_list, black_list) - self.assertEqual(amp_lists.gray_list, gray_list) - - def test_amp_lists_1(self): - white_list = ( - copy.copy(paddle.static.amp.fp16_lists.white_list) - | paddle.static.amp.fp16_lists._only_supported_fp16_list - ) - black_list = copy.copy( - paddle.static.amp.fp16_lists.black_list - | paddle.static.amp.fp16_lists._extra_black_list - ) - gray_list = copy.copy(paddle.static.amp.fp16_lists.gray_list) - - # 1. w={'exp}, b=None - white_list.add('exp') - black_list.remove('exp') - - amp_lists = paddle.static.amp.AutoMixedPrecisionLists({'exp'}) - self.assertEqual(amp_lists.white_list, white_list) - self.assertEqual(amp_lists.black_list, black_list) - self.assertEqual(amp_lists.gray_list, gray_list) - - def test_amp_lists_2(self): - white_list = ( - copy.copy(paddle.static.amp.fp16_lists.white_list) - | paddle.static.amp.fp16_lists._only_supported_fp16_list - ) - black_list = copy.copy( - paddle.static.amp.fp16_lists.black_list - | paddle.static.amp.fp16_lists._extra_black_list - ) - gray_list = copy.copy(paddle.static.amp.fp16_lists.gray_list) - - # 2. w={'tanh'}, b=None - white_list.add('tanh') - gray_list.remove('tanh') - - amp_lists = paddle.static.amp.AutoMixedPrecisionLists({'tanh'}) - self.assertEqual(amp_lists.white_list, white_list) - self.assertEqual(amp_lists.black_list, black_list) - self.assertEqual(amp_lists.gray_list, gray_list) - - def test_amp_lists_3(self): - white_list = ( - copy.copy(paddle.static.amp.fp16_lists.white_list) - | paddle.static.amp.fp16_lists._only_supported_fp16_list - ) - black_list = copy.copy( - paddle.static.amp.fp16_lists.black_list - | paddle.static.amp.fp16_lists._extra_black_list - ) - gray_list = copy.copy(paddle.static.amp.fp16_lists.gray_list) - - # 3. w={'lstm'}, b=None - white_list.add('lstm') - - amp_lists = paddle.static.amp.AutoMixedPrecisionLists({'lstm'}) - self.assertEqual(amp_lists.white_list, white_list) - self.assertEqual(amp_lists.black_list, black_list) - self.assertEqual(amp_lists.gray_list, gray_list) - - def test_amp_lists_4(self): - white_list = ( - copy.copy(paddle.static.amp.fp16_lists.white_list) - | paddle.static.amp.fp16_lists._only_supported_fp16_list - ) - black_list = copy.copy( - paddle.static.amp.fp16_lists.black_list - | paddle.static.amp.fp16_lists._extra_black_list - ) - gray_list = copy.copy(paddle.static.amp.fp16_lists.gray_list) - - # 4. w=None, b={'conv2d'} - white_list.remove('conv2d') - black_list.add('conv2d') - - amp_lists = paddle.static.amp.AutoMixedPrecisionLists( - custom_black_list={'conv2d'} - ) - self.assertEqual(amp_lists.white_list, white_list) - self.assertEqual(amp_lists.black_list, black_list) - self.assertEqual(amp_lists.gray_list, gray_list) - - def test_amp_lists_5(self): - white_list = ( - copy.copy(paddle.static.amp.fp16_lists.white_list) - | paddle.static.amp.fp16_lists._only_supported_fp16_list - ) - black_list = copy.copy( - paddle.static.amp.fp16_lists.black_list - | paddle.static.amp.fp16_lists._extra_black_list - ) - gray_list = copy.copy(paddle.static.amp.fp16_lists.gray_list) - - # 5. w=None, b={'tanh'} - black_list.add('tanh') - gray_list.remove('tanh') - - amp_lists = paddle.static.amp.AutoMixedPrecisionLists( - custom_black_list={'tanh'} - ) - self.assertEqual(amp_lists.white_list, white_list) - self.assertEqual(amp_lists.black_list, black_list) - self.assertEqual(amp_lists.gray_list, gray_list) - - def test_amp_lists_6(self): - white_list = ( - copy.copy(paddle.static.amp.fp16_lists.white_list) - | paddle.static.amp.fp16_lists._only_supported_fp16_list - ) - black_list = copy.copy( - paddle.static.amp.fp16_lists.black_list - | paddle.static.amp.fp16_lists._extra_black_list - ) - gray_list = copy.copy(paddle.static.amp.fp16_lists.gray_list) - - # 6. w=None, b={'lstm'} - black_list.add('lstm') - - amp_lists = paddle.static.amp.AutoMixedPrecisionLists( - custom_black_list={'lstm'} - ) - self.assertEqual(amp_lists.white_list, white_list) - self.assertEqual(amp_lists.black_list, black_list) - self.assertEqual(amp_lists.gray_list, gray_list) - - def test_amp_lists_7(self): - # 7. w={'lstm'} b={'lstm'} - # raise ValueError - self.assertRaises( - ValueError, - paddle.static.amp.AutoMixedPrecisionLists, - {'lstm'}, - {'lstm'}, - ) - def test_vgg_cuda(self): with self.scope_prog_guard(): self.main('vgg', use_cuda=True) diff --git a/test/deprecated/ir/test_ir_embedding_eltwise_layernorm_fuse_pass.py b/test/deprecated/ir/test_ir_embedding_eltwise_layernorm_fuse_pass_deprecated.py similarity index 99% rename from test/deprecated/ir/test_ir_embedding_eltwise_layernorm_fuse_pass.py rename to test/deprecated/ir/test_ir_embedding_eltwise_layernorm_fuse_pass_deprecated.py index 6b2af9ace72bf4..68c109120511ec 100644 --- a/test/deprecated/ir/test_ir_embedding_eltwise_layernorm_fuse_pass.py +++ b/test/deprecated/ir/test_ir_embedding_eltwise_layernorm_fuse_pass_deprecated.py @@ -24,6 +24,8 @@ from paddle import base from paddle.base import core +paddle.enable_static() + class EmbEltwiseLayerNormFusePassTest(PassTest): def setUp(self): diff --git a/test/deprecated/ir/test_ir_fc_fuse_pass.py b/test/deprecated/ir/test_ir_fc_fuse_pass_deprecated.py similarity index 98% rename from test/deprecated/ir/test_ir_fc_fuse_pass.py rename to test/deprecated/ir/test_ir_fc_fuse_pass_deprecated.py index e7dde8b9f6c4c8..79aa2f6efc9eb3 100644 --- a/test/deprecated/ir/test_ir_fc_fuse_pass.py +++ b/test/deprecated/ir/test_ir_fc_fuse_pass_deprecated.py @@ -24,6 +24,8 @@ from paddle import base from paddle.base import core +paddle.enable_static() + class FCFusePassTest(PassTest): def setUp(self): diff --git a/test/deprecated/ir/test_ir_generate_pass.py b/test/deprecated/ir/test_ir_generate_pass_deprecated.py similarity index 100% rename from test/deprecated/ir/test_ir_generate_pass.py rename to test/deprecated/ir/test_ir_generate_pass_deprecated.py diff --git a/test/deprecated/ir/test_ir_graph_to_program_pass.py b/test/deprecated/ir/test_ir_graph_to_program_pass_deprecated.py similarity index 100% rename from test/deprecated/ir/test_ir_graph_to_program_pass.py rename to test/deprecated/ir/test_ir_graph_to_program_pass_deprecated.py diff --git a/test/deprecated/ir/test_ir_preln_residual_bias_fuse_pass.py b/test/deprecated/ir/test_ir_preln_residual_bias_fuse_pass_deprecated.py similarity index 100% rename from test/deprecated/ir/test_ir_preln_residual_bias_fuse_pass.py rename to test/deprecated/ir/test_ir_preln_residual_bias_fuse_pass_deprecated.py diff --git a/test/deprecated/ir/test_ir_skip_layernorm_pass.py b/test/deprecated/ir/test_ir_skip_layernorm_pass_deprecated.py similarity index 100% rename from test/deprecated/ir/test_ir_skip_layernorm_pass.py rename to test/deprecated/ir/test_ir_skip_layernorm_pass_deprecated.py diff --git a/test/deprecated/ir/test_ir_yolo_box_pass.py b/test/deprecated/ir/test_ir_yolo_box_pass_deprecated.py similarity index 100% rename from test/deprecated/ir/test_ir_yolo_box_pass.py rename to test/deprecated/ir/test_ir_yolo_box_pass_deprecated.py diff --git a/test/deprecated/ir/test_op_input_grad_semantic.py b/test/deprecated/ir/test_op_input_grad_semantic_deprecated.py similarity index 100% rename from test/deprecated/ir/test_op_input_grad_semantic.py rename to test/deprecated/ir/test_op_input_grad_semantic_deprecated.py diff --git a/test/deprecated/legacy_test/CMakeLists.txt b/test/deprecated/legacy_test/CMakeLists.txt index a298606604b55d..3b14bff1daea82 100644 --- a/test/deprecated/legacy_test/CMakeLists.txt +++ b/test/deprecated/legacy_test/CMakeLists.txt @@ -33,17 +33,18 @@ set(MIXED_DIST_TEST_OPS ${DIST_TEST_OPS}) list(APPEND MIXED_DIST_TEST_OPS test_simple_dist_transpiler) list(APPEND MIXED_DIST_TEST_OPS test_communicator_async) list(APPEND MIXED_DIST_TEST_OPS test_communicator_ps_gpu) -list(APPEND MIXED_DIST_TEST_OPS test_communicator_geo) +list(APPEND MIXED_DIST_TEST_OPS test_communicator_geo_deprecated) list(APPEND MIXED_DIST_TEST_OPS test_fleet_launch_ascend) list(APPEND MIXED_DIST_TEST_OPS test_ascend_group) list(APPEND MIXED_DIST_TEST_OPS test_fleet_api_input) list(APPEND MIXED_DIST_TEST_OPS test_fleet_base) -list(APPEND MIXED_DIST_TEST_OPS test_fleet_base_2) -list(APPEND MIXED_DIST_TEST_OPS test_fleet_base_3) +list(APPEND MIXED_DIST_TEST_OPS test_fleet_base_deprecated) +list(APPEND MIXED_DIST_TEST_OPS test_fleet_base_2_deprecated) +list(APPEND MIXED_DIST_TEST_OPS test_fleet_base_3_deprecated) list(APPEND MIXED_DIST_TEST_OPS test_fleet_auto) -list(APPEND MIXED_DIST_TEST_OPS test_auto_parallel_partitioner) -list(APPEND MIXED_DIST_TEST_OPS test_auto_parallel_partitioner_gpt) +list(APPEND MIXED_DIST_TEST_OPS test_auto_parallel_partitioner_deprecated) +list(APPEND MIXED_DIST_TEST_OPS test_auto_parallel_partitioner_gpt_deprecated) list(APPEND MIXED_DIST_TEST_OPS test_auto_parallel_searcher) list(APPEND MIXED_DIST_TEST_OPS test_auto_parallel_reshard) list(APPEND MIXED_DIST_TEST_OPS test_auto_parallel_dist_tensor) @@ -100,11 +101,11 @@ if(WIN32) list(REMOVE_ITEM TEST_OPS test_trainer_desc) list(REMOVE_ITEM TEST_OPS test_checkpoint_notify_op) list(REMOVE_ITEM TEST_OPS test_downpoursgd_deprecated) - list(REMOVE_ITEM TEST_OPS test_fleet) - list(REMOVE_ITEM TEST_OPS test_fleet_nocvm_1) + list(REMOVE_ITEM TEST_OPS test_fleet_deprecated) + list(REMOVE_ITEM TEST_OPS test_fleet_nocvm_1_deprecated) list(REMOVE_ITEM TEST_OPS test_fleet_rolemaker) list(REMOVE_ITEM TEST_OPS test_fleet_rolemaker_3) - list(REMOVE_ITEM TEST_OPS test_fleet_unitaccessor) + list(REMOVE_ITEM TEST_OPS test_fleet_unitaccessor_deprecated) list(REMOVE_ITEM TEST_OPS test_ps_dispatcher) list(REMOVE_ITEM TEST_OPS test_ir_memory_optimize_nlp) list(REMOVE_ITEM TEST_OPS test_nvprof) @@ -115,7 +116,7 @@ endif() if(NOT WITH_DISTRIBUTE OR WIN32) # DISTRIBUTE related - list(REMOVE_ITEM TEST_OPS test_avoid_twice_initialization) + list(REMOVE_ITEM TEST_OPS test_avoid_twice_initialization_deprecated) list(REMOVE_ITEM TEST_OPS test_fleet_metric) list(REMOVE_ITEM TEST_OPS test_fleet_ps) list(REMOVE_ITEM TEST_OPS test_fleet_rolemaker_2) @@ -161,8 +162,8 @@ if((NOT WITH_GPU) AND (NOT WITH_ROCM)) # TODO(shenliang03): batch_fc_op support CPU device in future # TODO(Yancey1989): parallel dygraph support CPU device in future list(REMOVE_ITEM TEST_OPS test_fleet_base_single) - list(REMOVE_ITEM TEST_OPS test_auto_parallel_partitioner) - list(REMOVE_ITEM TEST_OPS test_auto_parallel_partitioner_gpt) + list(REMOVE_ITEM TEST_OPS test_auto_parallel_partitioner_deprecated) + list(REMOVE_ITEM TEST_OPS test_auto_parallel_partitioner_gpt_deprecated) list(REMOVE_ITEM TEST_OPS test_auto_parallel_searcher) list(REMOVE_ITEM TEST_OPS test_auto_parallel_reshard) list(REMOVE_ITEM TEST_OPS test_auto_parallel_dist_tensor) @@ -199,8 +200,8 @@ list(REMOVE_ITEM TEST_OPS decorator_helper) if(APPLE) if(NOT WITH_DISTRIBUTE) - list(REMOVE_ITEM TEST_OPS test_desc_clone) - list(REMOVE_ITEM TEST_OPS test_program_code) + list(REMOVE_ITEM TEST_OPS test_desc_clone_deprecated) + list(REMOVE_ITEM TEST_OPS test_program_code_deprecated) endif() message( WARNING @@ -397,9 +398,9 @@ function(parallel_bash_test_modules TARGET_NAME) endif() endfunction() -list(REMOVE_ITEM TEST_OPS test_feed_data_check_shape_type) +list(REMOVE_ITEM TEST_OPS test_feed_data_check_shape_type_deprecated) list(REMOVE_ITEM TEST_OPS test_fetch_lod_tensor_array) -list(REMOVE_ITEM TEST_OPS test_data_norm_op) +list(REMOVE_ITEM TEST_OPS test_data_norm_op_deprecated) list(REMOVE_ITEM TEST_OPS test_imperative_mnist_sorted_gradient) list(REMOVE_ITEM TEST_OPS test_imperative_mnist) list(REMOVE_ITEM TEST_OPS test_layers_deprecated) @@ -410,7 +411,7 @@ list(REMOVE_ITEM TEST_OPS test_basic_gru_api) list(REMOVE_ITEM TEST_OPS test_basic_gru_unit_op) list(REMOVE_ITEM TEST_OPS test_basic_lstm_api) list(REMOVE_ITEM TEST_OPS test_basic_lstm_unit_op) -list(REMOVE_ITEM TEST_OPS test_fuse_bn_act_pass) +list(REMOVE_ITEM TEST_OPS test_fuse_bn_act_pass_deprecated) # disable this unittest temporarily list(REMOVE_ITEM TEST_OPS test_imperative_data_loader_exception) @@ -425,6 +426,7 @@ endif() if(APPLE OR WIN32) list(REMOVE_ITEM TEST_OPS test_dataset) + list(REMOVE_ITEM TEST_OPS test_dataset_deprecated) list(REMOVE_ITEM TEST_OPS test_dataset_dataloader) list(REMOVE_ITEM TEST_OPS test_imperative_data_loader_process) list(REMOVE_ITEM TEST_OPS test_imperative_data_loader_exit_func) @@ -450,6 +452,7 @@ 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 test_gather_nd_op test_slice_op) +set(TEST_OPS_WITH_GC test_gather_nd_op test_slice_op_deprecated) foreach(TEST_OP ${TEST_OPS_WITH_GC}) list(REMOVE_ITEM TEST_OPS ${TEST_OP}) @@ -505,7 +508,8 @@ endif() set_tests_properties(test_conv2d_api_deprecated PROPERTIES LABELS "RUN_TYPE=EXCLUSIVE") -set_tests_properties(test_conv_nn_grad PROPERTIES LABELS "RUN_TYPE=EXCLUSIVE") +set_tests_properties(test_conv_nn_grad_deprecated + PROPERTIES LABELS "RUN_TYPE=EXCLUSIVE") if(WITH_DISTRIBUTE) # FIXME(typhoonzero): add these tests back list(REMOVE_ITEM DIST_TEST_OPS "test_dist_transformer") @@ -533,19 +537,23 @@ if(WITH_DISTRIBUTE) py_test_modules(test_communicator_async MODULES test_communicator_async ENVS ${dist_ENVS}) - py_test_modules(test_communicator_geo MODULES test_communicator_geo ENVS - ${dist_ENVS}) + py_test_modules(test_communicator_geo_deprecated MODULES + test_communicator_geo_deprecated ENVS ${dist_ENVS}) if(NOT APPLE) py_test_modules(test_fleet_base MODULES test_fleet_base ENVS ${dist_ENVS}) - py_test_modules(test_fleet_base_2 MODULES test_fleet_base_2 ENVS - ${dist_ENVS}) - py_test_modules(test_fleet_base_3 MODULES test_fleet_base_3 ENVS - ${dist_ENVS}) + py_test_modules(test_fleet_base_deprecated MODULES + test_fleet_base_deprecated ENVS ${dist_ENVS}) + py_test_modules(test_fleet_base_2_deprecated MODULES + test_fleet_base_2_deprecated ENVS ${dist_ENVS}) + py_test_modules(test_fleet_base_3_deprecated MODULES + test_fleet_base_3_deprecated ENVS ${dist_ENVS}) if(NOT WIN32) - py_test_modules(test_auto_parallel_partitioner MODULES - test_auto_parallel_partitioner ENVS ${dist_ENVS}) - py_test_modules(test_auto_parallel_partitioner_gpt MODULES - test_auto_parallel_partitioner_gpt ENVS ${dist_ENVS}) + py_test_modules( + test_auto_parallel_partitioner_deprecated MODULES + test_auto_parallel_partitioner_deprecated ENVS ${dist_ENVS}) + py_test_modules( + test_auto_parallel_partitioner_gpt_deprecated MODULES + test_auto_parallel_partitioner_gpt_deprecated ENVS ${dist_ENVS}) py_test_modules(test_auto_parallel_searcher MODULES test_auto_parallel_searcher ENVS ${dist_ENVS}) py_test_modules(test_auto_parallel_reshard MODULES @@ -593,22 +601,24 @@ if(WITH_DISTRIBUTE) endif() if(WIN32) - py_test_modules(test_feed_data_check_shape_type MODULES - test_feed_data_check_shape_type ENVS CUDA_VISIBLE_DEVICES=0) + py_test_modules( + test_feed_data_check_shape_type_deprecated MODULES + test_feed_data_check_shape_type_deprecated ENVS CUDA_VISIBLE_DEVICES=0) py_test_modules(test_fetch_lod_tensor_array MODULES test_fetch_lod_tensor_array ENVS CUDA_VISIBLE_DEVICES=0) else() - py_test_modules(test_feed_data_check_shape_type MODULES - test_feed_data_check_shape_type) + py_test_modules(test_feed_data_check_shape_type_deprecated MODULES + test_feed_data_check_shape_type_deprecated) py_test_modules(test_fetch_lod_tensor_array MODULES test_fetch_lod_tensor_array) endif() -py_test_modules(test_data_norm_op MODULES test_data_norm_op) +py_test_modules(test_data_norm_op_deprecated MODULES + test_data_norm_op_deprecated) py_test_modules( - test_fuse_bn_act_pass + test_fuse_bn_act_pass_deprecated MODULES - test_fuse_bn_act_pass + test_fuse_bn_act_pass_deprecated ENVS FLAGS_cudnn_deterministic=1 FLAGS_cudnn_batchnorm_spatial_persistent=1 @@ -621,8 +631,8 @@ if(NOT WIN32) endif() set_tests_properties( - test_data_norm_op test_dataloader_keep_order test_dataloader_unkeep_order - PROPERTIES LABELS "RUN_TYPE=DIST") + test_data_norm_op_deprecated test_dataloader_keep_order_deprecated + test_dataloader_unkeep_order_deprecated PROPERTIES LABELS "RUN_TYPE=DIST") if(NOT WIN32) set_tests_properties(test_multiprocess_reader_exception @@ -632,35 +642,38 @@ endif() # setting timeout value as 15S set_tests_properties(test_cross_op PROPERTIES TIMEOUT 120) -set_tests_properties(test_imperative_lod_tensor_to_selected_rows +set_tests_properties(test_imperative_lod_tensor_to_selected_rows_deprecated PROPERTIES TIMEOUT 200) -set_tests_properties(test_deformable_conv_op PROPERTIES TIMEOUT 200) +set_tests_properties(test_deformable_conv_op_deprecated PROPERTIES TIMEOUT 200) set_tests_properties(test_inplace_softmax_with_cross_entropy PROPERTIES TIMEOUT 120) -set_tests_properties(test_regularizer_api PROPERTIES TIMEOUT 150) +set_tests_properties(test_regularizer_api_deprecated PROPERTIES TIMEOUT 150) if(NOT WIN32) if(WITH_NV_JETSON) set_tests_properties(test_ir_memory_optimize_nlp PROPERTIES TIMEOUT 1200) endif() endif() -set_tests_properties(test_add_reader_dependency PROPERTIES TIMEOUT 120) +set_tests_properties(test_add_reader_dependency_deprecated PROPERTIES TIMEOUT + 120) set_tests_properties(test_bilateral_slice_op PROPERTIES TIMEOUT 120) set_tests_properties(test_fleet_util PROPERTIES TIMEOUT 120) set_tests_properties(test_imperative_transformer_sorted_gradient PROPERTIES TIMEOUT 120) set_tests_properties(test_static_save_load PROPERTIES TIMEOUT 250) if(WITH_NV_JETSON) - set_tests_properties(test_conv3d_transpose_part2_op PROPERTIES TIMEOUT 1200) - set_tests_properties(test_layer_norm_op PROPERTIES TIMEOUT 1500) + set_tests_properties(test_conv3d_transpose_part2_op_deprecated + PROPERTIES TIMEOUT 1200) + set_tests_properties(test_layer_norm_op_deprecated PROPERTIES TIMEOUT 1500) else() - set_tests_properties(test_conv3d_transpose_part2_op PROPERTIES TIMEOUT 120) - set_tests_properties(test_layer_norm_op PROPERTIES TIMEOUT 250) + set_tests_properties(test_conv3d_transpose_part2_op_deprecated + PROPERTIES TIMEOUT 120) + set_tests_properties(test_layer_norm_op_deprecated PROPERTIES TIMEOUT 250) endif() set_tests_properties(test_imperative_selected_rows_to_lod_tensor PROPERTIES TIMEOUT 200) -set_tests_properties(test_argsort_op PROPERTIES TIMEOUT 120) +set_tests_properties(test_argsort_op_deprecated PROPERTIES TIMEOUT 120) set_tests_properties(test_gather_nd_op PROPERTIES TIMEOUT 120) set_tests_properties(test_imperative_ptb_rnn_sorted_gradient PROPERTIES TIMEOUT 120) @@ -669,23 +682,27 @@ set_tests_properties(test_masked_select_op PROPERTIES TIMEOUT 120) set_tests_properties(test_sigmoid_cross_entropy_with_logits_op PROPERTIES TIMEOUT 120) set_tests_properties(test_imperative_optimizer_v2 PROPERTIES TIMEOUT 150) -set_tests_properties(test_sgd_op PROPERTIES TIMEOUT 250) +set_tests_properties(test_sgd_op_deprecated PROPERTIES TIMEOUT 250) set_tests_properties(test_generator_dataloader_deprecated PROPERTIES TIMEOUT 120) set_tests_properties(test_reduce_op PROPERTIES TIMEOUT 500) -set_tests_properties(test_conv_nn_grad PROPERTIES TIMEOUT 220) -set_tests_properties(test_program_prune_backward PROPERTIES TIMEOUT 120) +set_tests_properties(test_conv_nn_grad_deprecated PROPERTIES TIMEOUT 220) +set_tests_properties(test_program_prune_backward_deprecated PROPERTIES TIMEOUT + 120) set_tests_properties(test_imperative_optimizer_v2 PROPERTIES TIMEOUT 250) -set_tests_properties(test_decoupled_py_reader PROPERTIES TIMEOUT 120) -set_tests_properties(test_fuse_bn_act_pass PROPERTIES TIMEOUT 120) +set_tests_properties(test_decoupled_py_reader_deprecated PROPERTIES TIMEOUT 120) +set_tests_properties(test_fuse_bn_act_pass_deprecated PROPERTIES TIMEOUT 120) set_tests_properties(test_conv2d_api_deprecated PROPERTIES TIMEOUT 120) set_tests_properties(test_dygraph_multi_forward PROPERTIES TIMEOUT 120) set_tests_properties(test_imperative_ocr_attention_model PROPERTIES TIMEOUT 120) set_tests_properties(test_imperative_mnist PROPERTIES TIMEOUT 120) -set_tests_properties(test_regularizer PROPERTIES TIMEOUT 150) +set_tests_properties(test_regularizer_deprecated PROPERTIES TIMEOUT 150) set_tests_properties(test_slice_op PROPERTIES TIMEOUT 120) -set_tests_properties(test_dataloader_keep_order PROPERTIES TIMEOUT 120) -set_tests_properties(test_dataloader_unkeep_order PROPERTIES TIMEOUT 120) +set_tests_properties(test_slice_op_deprecated PROPERTIES TIMEOUT 120) +set_tests_properties(test_dataloader_keep_order_deprecated PROPERTIES TIMEOUT + 120) +set_tests_properties(test_dataloader_unkeep_order_deprecated PROPERTIES TIMEOUT + 120) set_tests_properties(test_reader_reset PROPERTIES TIMEOUT 120) set_tests_properties(test_split_program PROPERTIES TIMEOUT 120) set_tests_properties(test_uniform_random_op_deprecated PROPERTIES TIMEOUT 60) @@ -711,13 +728,15 @@ set(TEST_CINN_OPS test_softmax_op test_reduce_op test_slice_op + test_slice_op_deprecated test_gather_nd_op test_scale_op - test_layer_norm_op + test_layer_norm_op_deprecated test_where_op test_arg_min_max_op test_scatter_nd_op test_instance_norm_op + test_instance_norm_op_deprecated test_cumsum_op test_erf_op) @@ -732,28 +751,31 @@ foreach(TEST_CINN_OP ${TEST_CINN_OPS}) endif() endforeach() -# In test_conditional_block, the sub block changes the dtype and place of the output variable. +# In test_conditional_block_deprecated, the sub block changes the dtype and place of the output variable. # The changed variable is used in the following op. Static build is not supported for this case. -set_tests_properties(test_conditional_block +set_tests_properties(test_conditional_block_deprecated PROPERTIES ENVIRONMENT "FLAGS_new_executor_static_build=0") # 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_adamw_op + test_adamw_op_deprecated test_arg_min_max_op - test_batch_norm_op + test_batch_norm_op_deprecated test_bincount_op - test_decoupled_py_reader + test_decoupled_py_reader_deprecated test_fetch_lod_tensor_array - test_fuse_bn_act_pass - test_layer_norm_op + test_fuse_bn_act_pass_deprecated + test_layer_norm_op_deprecated test_lookup_table_v2_op_deprecated test_momentum_op - test_nce + test_momentum_op_deprecated + test_nce_deprecated test_reduce_op test_sparse_conv_op test_sparse_norm_op test_tensor_array_to_tensor + test_tensor_array_to_tensor_deprecated test_unique test_one_hot_v2_op) diff --git a/test/deprecated/legacy_test/run_server_for_communicator_geo.py b/test/deprecated/legacy_test/run_server_for_communicator_geo.py index c384459a0ffbc1..055ea5faf04fa9 100644 --- a/test/deprecated/legacy_test/run_server_for_communicator_geo.py +++ b/test/deprecated/legacy_test/run_server_for_communicator_geo.py @@ -13,8 +13,9 @@ # limitations under the License. import os - -from test_communicator_geo import TestCommunicatorGeoEnd2End +from test.deprecated.legacy_test.test_communicator_geo_deprecated import ( + TestCommunicatorGeoEnd2End, +) import paddle diff --git a/test/deprecated/legacy_test/test_adam_op.py b/test/deprecated/legacy_test/test_adam_op.py index 0693d4f6643569..50caa254576719 100644 --- a/test/deprecated/legacy_test/test_adam_op.py +++ b/test/deprecated/legacy_test/test_adam_op.py @@ -648,39 +648,6 @@ def test_check_output(self): class TestAdamOpV2(unittest.TestCase): - def test_adam_op(self): - place = base.CPUPlace() - shape = [2, 3, 8, 8] - 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) - - beta1 = paddle.static.create_global_var( - shape=[1], value=0.85, dtype='float32', persistable=True - ) - beta2 = paddle.static.create_global_var( - shape=[1], value=0.95, dtype='float32', persistable=True - ) - betas = [beta1, beta2] - opt = paddle.optimizer.Adam( - learning_rate=1e-5, - beta1=beta1, - beta2=beta2, - weight_decay=0.01, - epsilon=1e-8, - ) - opt.minimize(loss) - - exe.run(startup) - data_np = np.random.random(shape).astype('float32') - rets = exe.run(train_prog, feed={"data": data_np}, fetch_list=[loss]) - assert rets[0] is not None - def test_pir_adam_op(self): with paddle.pir_utils.IrGuard(): place = base.CPUPlace() diff --git a/test/deprecated/legacy_test/test_adam_op_deprecated.py b/test/deprecated/legacy_test/test_adam_op_deprecated.py new file mode 100644 index 00000000000000..fd6970fae84628 --- /dev/null +++ b/test/deprecated/legacy_test/test_adam_op_deprecated.py @@ -0,0 +1,60 @@ +# Copyright (c) 2018 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 base + + +class TestAdamOpV2(unittest.TestCase): + def test_adam_op(self): + place = base.CPUPlace() + shape = [2, 3, 8, 8] + 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) + + beta1 = paddle.static.create_global_var( + shape=[1], value=0.85, dtype='float32', persistable=True + ) + beta2 = paddle.static.create_global_var( + shape=[1], value=0.95, dtype='float32', persistable=True + ) + betas = [beta1, beta2] + opt = paddle.optimizer.Adam( + learning_rate=1e-5, + beta1=beta1, + beta2=beta2, + weight_decay=0.01, + epsilon=1e-8, + ) + opt.minimize(loss) + + exe.run(startup) + data_np = np.random.random(shape).astype('float32') + rets = exe.run(train_prog, feed={"data": data_np}, fetch_list=[loss]) + assert rets[0] is not None + + +if __name__ == "__main__": + paddle.enable_static() + unittest.main() diff --git a/test/deprecated/legacy_test/test_adamax_api_deprecated.py b/test/deprecated/legacy_test/test_adamax_api_deprecated.py new file mode 100644 index 00000000000000..6f1d806be7eeaf --- /dev/null +++ b/test/deprecated/legacy_test/test_adamax_api_deprecated.py @@ -0,0 +1,54 @@ +# Copyright (c) 2020 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 base + + +class TestAdamaxAPI(unittest.TestCase): + def test_adamax_api(self): + paddle.enable_static() + place = base.CPUPlace() + shape = [2, 3, 8, 8] + 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) + beta1 = 0.85 + beta2 = 0.95 + opt = paddle.optimizer.Adamax( + learning_rate=1e-5, + beta1=beta1, + beta2=beta2, + weight_decay=0.01, + epsilon=1e-8, + ) + opt.minimize(loss) + + exe.run(startup) + data_np = np.random.random(shape).astype('float32') + rets = exe.run(train_prog, feed={"data": data_np}, fetch_list=[loss]) + assert rets[0] is not None + + +if __name__ == "__main__": + unittest.main() diff --git a/test/deprecated/legacy_test/test_adamw_op.py b/test/deprecated/legacy_test/test_adamw_op.py index 1c901e8d4baf57..53465c1dff73b5 100644 --- a/test/deprecated/legacy_test/test_adamw_op.py +++ b/test/deprecated/legacy_test/test_adamw_op.py @@ -262,41 +262,6 @@ def test_adamw_op_coverage(self): ) assert adam.__str__() is not None - def test_adamw_op(self): - paddle.enable_static() - place = base.CPUPlace() - shape = [2, 3, 8, 8] - 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) - - beta1 = paddle.static.create_global_var( - shape=[1], value=0.85, dtype='float32', persistable=True - ) - beta2 = paddle.static.create_global_var( - shape=[1], value=0.95, dtype='float32', persistable=True - ) - betas = [beta1, beta2] - opt = paddle.optimizer.AdamW( - learning_rate=1e-5, - beta1=beta1, - beta2=beta2, - weight_decay=0.01, - epsilon=1e-8, - ) - opt.minimize(loss) - - exe.run(startup) - data_np = np.random.random(shape).astype('float32') - rets = exe.run(train_prog, feed={"data": data_np}, fetch_list=[loss]) - assert rets[0] is not None - paddle.disable_static() - def test_pir_adam_op(self): with paddle.pir_utils.IrGuard(): place = base.CPUPlace() diff --git a/test/deprecated/legacy_test/test_adamw_op_deprecated.py b/test/deprecated/legacy_test/test_adamw_op_deprecated.py new file mode 100644 index 00000000000000..c5f5aa5453bbff --- /dev/null +++ b/test/deprecated/legacy_test/test_adamw_op_deprecated.py @@ -0,0 +1,61 @@ +# Copyright (c) 2020 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 base + + +class TestAdamWOp(unittest.TestCase): + def test_adamw_op(self): + paddle.enable_static() + place = base.CPUPlace() + shape = [2, 3, 8, 8] + 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) + + beta1 = paddle.static.create_global_var( + shape=[1], value=0.85, dtype='float32', persistable=True + ) + beta2 = paddle.static.create_global_var( + shape=[1], value=0.95, dtype='float32', persistable=True + ) + betas = [beta1, beta2] + opt = paddle.optimizer.AdamW( + learning_rate=1e-5, + beta1=beta1, + beta2=beta2, + weight_decay=0.01, + epsilon=1e-8, + ) + opt.minimize(loss) + + exe.run(startup) + data_np = np.random.random(shape).astype('float32') + rets = exe.run(train_prog, feed={"data": data_np}, fetch_list=[loss]) + assert rets[0] is not None + paddle.disable_static() + + +if __name__ == "__main__": + unittest.main() diff --git a/test/deprecated/legacy_test/test_add_reader_dependency.py b/test/deprecated/legacy_test/test_add_reader_dependency_deprecated.py similarity index 99% rename from test/deprecated/legacy_test/test_add_reader_dependency.py rename to test/deprecated/legacy_test/test_add_reader_dependency_deprecated.py index 9dfd4a500d1f70..e844d26679dc2a 100644 --- a/test/deprecated/legacy_test/test_add_reader_dependency.py +++ b/test/deprecated/legacy_test/test_add_reader_dependency_deprecated.py @@ -21,6 +21,8 @@ from paddle import base from paddle.base.layer_helper import LayerHelper +paddle.enable_static() + def inplace_add(x, bias): helper = LayerHelper('scale', **locals()) diff --git a/test/deprecated/legacy_test/test_argsort_op_deprecated.py b/test/deprecated/legacy_test/test_argsort_op_deprecated.py new file mode 100644 index 00000000000000..96cd7612670824 --- /dev/null +++ b/test/deprecated/legacy_test/test_argsort_op_deprecated.py @@ -0,0 +1,355 @@ +# Copyright (c) 2020 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 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() + + +class PyArgsort: + def __init__(self, input_shape, axis, descending, dtype): + self.x = np.random.random(input_shape).astype(dtype) + self.label = np.random.random(input_shape).astype(dtype) + if axis < 0: + self.axis = axis + len(self.x.shape) + else: + self.axis = axis + self.descending = descending + + def forward(self): + if self.descending: + self.indices = np.flip( + np.argsort(self.x, kind='quicksort', axis=self.axis), self.axis + ) + self.sorted_x = np.flip( + np.sort(self.x, kind='quicksort', axis=self.axis), self.axis + ) + else: + self.indices = np.argsort(self.x, kind='quicksort', axis=self.axis) + self.sorted_x = np.sort(self.x, kind='quicksort', axis=self.axis) + self.loss = self.sorted_x * self.label + self.loss = np.sum(self.loss) + out = ( + np.array(self.indices, dtype=self.indices.dtype), + np.array(self.sorted_x, dtype=self.sorted_x.dtype), + np.array(self.loss, dtype=self.loss.dtype), + ) + return out + + +def create_tensor(np_data, place): + tensor = core.LoDTensor() + tensor.set(np_data, place) + return tensor + + +class TestArgsortOpCPU(unittest.TestCase): + def setup_program(self): + self.main_program = Program() + self.startup_program = Program() + self.init_place() + + def setUp(self): + paddle.enable_static() + self.init_axis() + self.init_datatype() + self.init_direction() + self.init_inputshape() + + self.setup_program() + self.feed_data_field = {"x", "label"} + self.grad_data_field = {"x"} + + self.py_argsort = PyArgsort( + self.input_shape, self.axis, self.descending, self.dtype + ) + + 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 + ) + x.stop_gradient = False + x.desc.set_need_check_feed(False) + label = paddle.static.data( + name="label", + shape=[-1] + list(self.input_shape), + dtype=self.dtype, + ) + label.desc.set_need_check_feed(False) + self.index = paddle.argsort( + x=x, axis=self.axis, descending=self.descending + ) + self.sorted_x = paddle.sort( + x=x, axis=self.axis, descending=self.descending + ) + self.sorted_x.stop_gradient = False + loss = paddle.multiply(self.sorted_x, label) + self.loss = paddle.sum(loss) + + def forward(self): + self.feed_map = { + x: create_tensor(getattr(self.py_argsort, x), self.place) + for x in self.feed_data_field + } + exe = Executor(self.place) + out = exe.run( + self.main_program, + feed=self.feed_map, + fetch_list=[self.index, self.sorted_x, self.loss], + ) + return out + + def backward(self): + self.feed_map = { + x: create_tensor(getattr(self.py_argsort, x), self.place) + for x in self.feed_data_field + } + fetch_list = [ + self.main_program.global_block().var(grad_var_name(x)) + for x in self.grad_data_field + ] + exe = Executor(self.place) + out = exe.run( + self.main_program, + feed=self.feed_map, + fetch_list=fetch_list, + return_numpy=False, + ) + return out + + def test_backward(self, numeric_grad_delta=1e-5, max_relative_error=1e-7): + self.check_forward() + + with base.program_guard(self.main_program, self.startup_program): + append_backward(self.loss) + + ana_grad = [np.array(x) for x in self.backward()] + + num_grad = self.get_numerical_gradient(delta=numeric_grad_delta) + self.assert_is_close( + num_grad, + ana_grad, + 'x', + max_relative_error=max_relative_error, + msg_prefix="Gradient Check On %s" % str(self.place), + ) + + def check_forward(self): + pd_outputs = self.forward() + py_outputs = self.py_argsort.forward() + for pd_output, py_output in zip(pd_outputs, py_outputs): + self.assertEqual(pd_output.shape, py_output.shape) + np.testing.assert_allclose( + pd_output, py_output, rtol=1e-05, atol=0, equal_nan=False + ) + + def get_numerical_gradient(self, delta=1e-7): + if self.dtype == 'float16': + delta = np.array(delta).astype(np.float16) + feed_list = [getattr(self.py_argsort, x) for x in self.grad_data_field] + grad_list = [np.zeros_like(x) for x in feed_list] + for feed, grad in zip(feed_list, grad_list): + for f, g in np.nditer([feed, grad], op_flags=['readwrite']): + o = float(f) + f[...] = o + delta + y_pos = self.forward()[2] + + f[...] = o - delta + y_neg = self.forward()[2] + + f[...] = o + dout_dfeed = (y_pos - y_neg) / (delta * 2) + g[...] = dout_dfeed + + return grad_list + + def assert_is_close( + self, + numeric_grads, + analytic_grads, + names, + max_relative_error, + msg_prefix, + ): + for a, b, name in zip(numeric_grads, analytic_grads, names): + abs_a = np.abs(a) + abs_a[abs_a < 1e-3] = 1 + + diff_mat = np.abs(a - b) / abs_a + max_diff = np.max(diff_mat) + + def err_msg(): + offset = np.argmax(diff_mat > max_relative_error) + return ( + "%s error, %s variable %s max gradient diff %f over limit %f, " + "the first error element is %d, expected %f, but got %f." + ) % ( + 'argsort', + msg_prefix, + name, + max_diff, + max_relative_error, + offset, + a.flatten()[offset], + b.flatten()[offset], + ) + + self.assertLessEqual(max_diff, max_relative_error, err_msg()) + + def init_axis(self): + self.axis = -1 + + def init_datatype(self): + self.dtype = "float64" + + def init_direction(self): + self.descending = False + + def init_inputshape(self): + self.input_shape = (2, 2, 2, 2, 3) + + def init_place(self): + self.place = core.CPUPlace() + + +class TestArgsortOpGPU(TestArgsortOpCPU): + def init_place(self): + if core.is_compiled_with_cuda(): + self.place = core.CUDAPlace(0) + else: + self.place = core.CPUPlace() + + +class TestArgsortOpAxis0CPU(TestArgsortOpCPU): + def init_axis(self): + self.axis = 0 + + +class TestArgsortOpAxis0GPU(TestArgsortOpGPU): + def init_axis(self): + self.axis = 0 + + +class TestArgsortOpAxis1CPU(TestArgsortOpCPU): + def init_axis(self): + self.axis = 1 + + +class TestArgsortOpAxis1GPU(TestArgsortOpGPU): + def init_axis(self): + self.axis = 1 + + +class TestArgsortOpAxis2CPU(TestArgsortOpCPU): + def init_axis(self): + self.axis = 2 + + +class TestArgsortOpAxis2GPU(TestArgsortOpGPU): + def init_axis(self): + self.axis = 2 + + +class TestArgsortOpAxisNeg1CPU(TestArgsortOpCPU): + def init_axis(self): + self.axis = -1 + + +class TestArgsortOpAxisNeg1GPU(TestArgsortOpGPU): + def init_axis(self): + self.axis = -1 + + +class TestArgsortOpAxisNeg2CPU(TestArgsortOpCPU): + def init_axis(self): + self.axis = -2 + + +class TestArgsortOpAxisNeg2GPU(TestArgsortOpGPU): + def init_axis(self): + self.axis = -2 + + +class TestArgsortOpDescendingAxisCPU(TestArgsortOpCPU): + def init_direction(self): + self.descending = True + + +class TestArgsortOpDescendingAxisGPU(TestArgsortOpGPU): + def init_direction(self): + self.descending = True + + +class TestArgsortOpDescendingAxis0CPU(TestArgsortOpAxis0CPU): + def init_direction(self): + self.descending = True + + +class TestArgsortOpDescendingAxis0GPU(TestArgsortOpAxis0GPU): + def init_direction(self): + self.descending = True + + +class TestArgsortOpDescendingAxis1CPU(TestArgsortOpAxis1CPU): + def init_direction(self): + self.descending = True + + +class TestArgsortOpDescendingAxis1GPU(TestArgsortOpAxis1GPU): + def init_direction(self): + self.descending = True + + +class TestArgsortOpDescendingAxis2CPU(TestArgsortOpAxis2CPU): + def init_direction(self): + self.descending = True + + +class TestArgsortOpDescendingAxis2GPU(TestArgsortOpAxis2GPU): + def init_direction(self): + self.descending = True + + +class TestArgsortOpDescendingAxisNeg1CPU(TestArgsortOpAxisNeg1CPU): + def init_direction(self): + self.descending = True + + +class TestArgsortOpDescendingAxisNeg1GPU(TestArgsortOpAxisNeg1GPU): + def init_direction(self): + self.descending = True + + +class TestArgsortOpDescendingAxisNeg2CPU(TestArgsortOpAxisNeg2CPU): + def init_direction(self): + self.descending = True + + +class TestArgsortOpDescendingAxisNeg2GPU(TestArgsortOpAxisNeg2GPU): + def init_direction(self): + self.descending = True + + +if __name__ == "__main__": + unittest.main() diff --git a/test/deprecated/legacy_test/test_auto_parallel_partitioner.py b/test/deprecated/legacy_test/test_auto_parallel_partitioner_deprecated.py similarity index 100% rename from test/deprecated/legacy_test/test_auto_parallel_partitioner.py rename to test/deprecated/legacy_test/test_auto_parallel_partitioner_deprecated.py diff --git a/test/deprecated/legacy_test/test_auto_parallel_partitioner_gpt.py b/test/deprecated/legacy_test/test_auto_parallel_partitioner_gpt_deprecated.py similarity index 100% rename from test/deprecated/legacy_test/test_auto_parallel_partitioner_gpt.py rename to test/deprecated/legacy_test/test_auto_parallel_partitioner_gpt_deprecated.py diff --git a/test/deprecated/legacy_test/test_avoid_twice_initialization.py b/test/deprecated/legacy_test/test_avoid_twice_initialization_deprecated.py similarity index 98% rename from test/deprecated/legacy_test/test_avoid_twice_initialization.py rename to test/deprecated/legacy_test/test_avoid_twice_initialization_deprecated.py index 5afb4cd20ccc3b..fdca1d76954f3e 100644 --- a/test/deprecated/legacy_test/test_avoid_twice_initialization.py +++ b/test/deprecated/legacy_test/test_avoid_twice_initialization_deprecated.py @@ -17,6 +17,8 @@ import paddle from paddle import base +paddle.enable_static() + class TestAvoidTwiceInitialization(unittest.TestCase): def test_avoid_twice_initialization(self): diff --git a/test/deprecated/legacy_test/test_backward.py b/test/deprecated/legacy_test/test_backward_deprecated.py similarity index 88% rename from test/deprecated/legacy_test/test_backward.py rename to test/deprecated/legacy_test/test_backward_deprecated.py index 04aeadc0382133..b3bef49bc1ab7c 100644 --- a/test/deprecated/legacy_test/test_backward.py +++ b/test/deprecated/legacy_test/test_backward_deprecated.py @@ -19,7 +19,6 @@ import paddle import paddle.nn.functional as F from paddle import base, static -from paddle.base import backward class BackwardNet: @@ -411,58 +410,6 @@ def test_gradient_with_optimizer(self): self._check_grad_op_name(forward_list, optimized_list) -# TODO(Aurelius84): add conditional network test -class ConditionalNet(BackwardNet): - def __init__(self): - super().__init__() - - -class TestBackwardUninitializedVariable(unittest.TestCase): - """this case is found in yolov5 while to_static. - gradient aggregation may cause sum a invalid variable. - """ - - def test(self): - paddle.enable_static() - main_prg, startup_prg = paddle.static.Program(), paddle.static.Program() - with paddle.static.program_guard(main_prg, startup_prg): - gt = paddle.static.data(name='gt', shape=[4], dtype='float32') - x = paddle.static.data(name='x', shape=[2], dtype='float32') - gt.stop_gradient = True - x.stop_gradient = False - gt = gt.reshape([4, 1]).reshape([4]) - loss = ( - paddle.nn.functional.binary_cross_entropy(x, gt[:2]) - + (gt[2:4] * x).sum() - ) - exe = paddle.static.Executor() - paddle.base.backward.gradients(loss, []) - exe.run(startup_prg) - # Optimizer - out = exe.run( - main_prg, - feed={ - 'gt': np.array([1.0, 1.0, 0.0, 0.0], dtype='float32'), - 'x': np.array([0.5, 0.5], dtype='float32'), - }, - fetch_list=[loss], - ) - print(out) - - -class TestStripGradSuffix(unittest.TestCase): - def test_strip_grad_suffix(self): - cases = ( - ('x@GRAD', 'x'), - ('x@GRAD@GRAD', 'x'), - ('x@GRAD@RENAME@1', 'x'), - ('x@GRAD_slice_0@GRAD', 'x@GRAD_slice_0'), - ('grad/grad/x@GRAD@RENAME@block0@1@GRAD', 'x'), - ) - for input_, desired in cases: - self.assertEqual(backward._strip_grad_suffix_(input_), desired) - - if __name__ == '__main__': paddle.enable_static() unittest.main() diff --git a/test/deprecated/legacy_test/test_batch_norm_op_deprecated.py b/test/deprecated/legacy_test/test_batch_norm_op_deprecated.py new file mode 100644 index 00000000000000..fa60d19ba56bdc --- /dev/null +++ b/test/deprecated/legacy_test/test_batch_norm_op_deprecated.py @@ -0,0 +1,526 @@ +# Copyright (c) 2018 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 unittest + +import numpy as np +from op_test import ( + _set_use_system_allocator, +) + +import paddle +from paddle import base +from paddle.base import core + +_set_use_system_allocator(True) + + +def _cal_mean_variance(x, epsilon, data_format): + assert data_format in ['NCHW', 'NHWC'] + x_shape = x.shape + if len(x_shape) == 3: + if data_format == "NCHW": # NCL -> NCL1 + x = np.reshape(x, (x_shape[0], x_shape[1], x_shape[2], 1)) + else: # NLC -> NL1C + x = np.reshape(x, (x_shape[0], x_shape[1], 1, x_shape[2])) + x_square = x * x + axis = (0, 2, 3) if data_format == 'NCHW' else (0, 1, 2) + C = x.shape[1] if data_format == 'NCHW' else x.shape[-1] + x_square_sum = np.sum(x_square, axis) + x_sum = np.sum(x, axis=axis) + element_count = np.size(x) / C + mean = x_sum / element_count + var = x_square_sum / element_count - mean * mean + return mean, var + + +def _reference_training(x, scale, offset, epsilon, data_format): + x_shape = x.shape + + if len(x_shape) == 3: + if data_format == "NCHW": # NCL -> NCL1 + x = np.reshape(x, (x_shape[0], x_shape[1], x_shape[2], 1)) + else: # NLC -> NL1C + x = np.reshape(x, (x_shape[0], x_shape[1], 1, x_shape[2])) + + if data_format == "NCHW": + n, c, h, w = x.shape + x_square = x * x + x_square_sum = np.sum(x_square, (0, 2, 3)) + x_sum = np.sum(x, axis=(0, 2, 3)) + element_count = np.size(x) / int(np.shape(x)[1]) + mean = x_sum / element_count + var = x_square_sum / element_count - mean * mean + mean_tile = np.reshape(mean, (1, c, 1, 1)) + mean_tile = np.tile(mean_tile, (n, 1, h, w)) + var_tile = np.reshape(var, (1, c, 1, 1)) + var_tile = np.tile(var_tile, (n, 1, h, w)) + normalized = (x - mean_tile) / np.sqrt(var_tile + epsilon) + scale_tile = np.reshape(scale, (1, c, 1, 1)) + scale_tile = np.tile(scale_tile, (n, 1, h, w)) + offset_tile = np.reshape(offset, (1, c, 1, 1)) + offset_tile = np.reshape(offset_tile, (1, c, 1, 1)) + y = normalized * scale_tile + offset_tile + elif data_format == "NHWC": + x_square = x * x + x_square_sum = np.sum(x_square, (0, 1, 2)) + x_sum = np.sum(x, axis=(0, 1, 2)) + element_count = np.size(x) / int(np.shape(x)[-1]) + mean = x_sum / element_count + var = x_square_sum / element_count - mean * mean + normalized = (x - mean) / np.sqrt(var + epsilon) + y = normalized * scale + offset + else: + raise ValueError("Unknown data order.") + + if len(x_shape) == 3: + y = np.reshape(y, x_shape) + return y, mean, var + + +def _reference_grad(x, y_grad, scale, mean, var, epsilon, data_format): + # Use the following formulas to calculate gradients: + # grad_scale = + # sum(grad_y * (x - mean)) * rsqrt(var + epsilon) + # + # grad_offset = sum(output_y) + # + # x_grad = + # 1/N * scale * rsqrt(var + epsilon) * (N * grad_y - sum(grad_y) - + # (x - mean) * sum(grad_y * (x - mean)) / (var + epsilon)) + + # transfer from (N, C, H, W) to (N, H, W, C) to simplify computation + if data_format != "NCHW" and data_format != "NHWC": + raise ValueError("Unknown data order.") + + x_shape = x.shape + if len(x_shape) == 3: + if data_format == "NCHW": # NCL -> NCL1 + x = np.reshape(x, (x_shape[0], x_shape[1], x_shape[2], 1)) + y_grad = np.reshape(y_grad, (x_shape[0], x_shape[1], x_shape[2], 1)) + else: # NLC -> NL1C + x = np.reshape(x, (x_shape[0], x_shape[1], 1, x_shape[2])) + y_grad = np.reshape(y_grad, (x_shape[0], x_shape[1], 1, x_shape[2])) + + if data_format == "NCHW": + x = np.transpose(x, (0, 2, 3, 1)) + y_grad = np.transpose(y_grad, (0, 2, 3, 1)) + + x_grad = ( + scale + * ( + y_grad + - np.mean(y_grad, axis=(0, 1, 2)) + - (x - mean) + * np.mean(y_grad * (x - mean), axis=(0, 1, 2)) + / (var + epsilon) + ) + / np.sqrt(var + epsilon) + ) + grad_scale = np.sum( + y_grad * (x - mean) / np.sqrt(var + epsilon), axis=(0, 1, 2) + ) + grad_offset = np.sum(y_grad, axis=(0, 1, 2)) + + # transfer back to N, C, H, W + if data_format == "NCHW": + x_grad = np.transpose(x_grad, (0, 3, 1, 2)) + x = np.transpose(x, (0, 3, 1, 2)) + y_grad = np.transpose(y_grad, (0, 3, 1, 2)) + + if len(x_shape) == 3: + x_grad = np.reshape(x_grad, x_shape) + + return x_grad, grad_scale, grad_offset + + +class TestBatchNormOpTraining(unittest.TestCase): + def setUp(self): + self.use_mkldnn = False + self.fuse_with_relu = False + self.data_formats = ["NCHW", "NHWC"] + self.momentum = 0.9 + self.use_momentum_variable = False + self.epsilon = 0.00001 + self.init_kernel_type() + self.init_test_case() + + def init_test_case(self): + self.use_global_stats = False + self.no_grad_set = set() + self.fetch_list = [ + 'y', + 'mean', + 'variance', + 'saved_mean', + 'saved_variance', + 'x@GRAD', + 'scale@GRAD', + 'bias@GRAD', + ] + + def __assert_close(self, tensor, np_array, msg, atol=1e-4): + np.allclose(np.array(tensor), np_array, atol=atol) + + def ref_forward_backward( + self, + x, + y_grad, + scale, + bias, + mean, + variance, + epsilon, + momentum, + shape, + data_layout, + ): + # run forward + y, saved_mean, var_ref = _reference_training( + x, scale, bias, epsilon, data_layout + ) + mean_out = saved_mean * (1.0 - momentum) + momentum * mean + variance_out = var_ref * (1.0 - momentum) + momentum * variance + saved_variance = 1.0 / np.sqrt(var_ref + epsilon) + # run backward + x_grad, scale_grad, bias_grad = _reference_grad( + x, y_grad, scale, saved_mean, var_ref, epsilon, data_layout + ) + + return ( + y, + mean_out, + variance_out, + saved_mean, + saved_variance, + x_grad, + scale_grad, + bias_grad, + ) + + def set_mean_variance(self, scale_shape, x, data_layout): + mean, variance = _cal_mean_variance(x, self.epsilon, data_layout) + mean_pre = np.zeros(scale_shape).astype(np.float32) + variance_pre = np.ones(scale_shape).astype(np.float32) + # computing global mean/variance for one step + if self.use_global_stats: + mom = self.momentum + mean = mean * (1.0 - mom) + mom * mean_pre + variance = variance * (1.0 - mom) + mom * variance_pre + return mean, variance + + def test_forward_backward(self): + def test_with_place(place, data_layout, shape): + # attr + epsilon = self.epsilon + momentum = self.momentum + if data_layout == "NCHW": + n, c, h, w = shape[0], shape[1], shape[2], shape[3] + else: + n, h, w, c = shape[0], shape[1], shape[2], shape[3] + scale_shape = [c] + + np.random.seed(123) + x = np.random.random_sample(shape).astype(np.float32) + scale = np.random.random_sample(scale_shape).astype(np.float32) + bias = np.random.random_sample(scale_shape).astype(np.float32) + mean, variance = self.set_mean_variance(scale_shape, x, data_layout) + y_grad = np.random.random_sample(shape).astype(np.float32) + momentum_var = np.array([momentum]).astype(np.float32) + + ( + y, + mean_out, + variance_out, + saved_mean, + saved_variance, + x_grad, + scale_grad, + bias_grad, + ) = self.ref_forward_backward( + x, + y_grad, + scale, + bias, + mean, + variance, + epsilon, + momentum, + shape, + data_layout, + ) + + var_dict = locals() + var_dict['y@GRAD'] = y_grad + var_dict['x@GRAD'] = x_grad + var_dict['scale@GRAD'] = scale_grad + var_dict['bias@GRAD'] = bias_grad + + var_names = [ + 'x', + 'scale', + 'bias', + 'mean', + 'variance', + 'y', + 'saved_mean', + 'saved_variance', + 'momentum_var', + ] + ground_truth = {name: var_dict[name] for name in var_names} + + program = base.Program() + with base.program_guard(program): + block = program.global_block() + for name in ground_truth: + block.create_var( + name=name, + dtype='float32', + shape=ground_truth[name].shape, + ) + inputs = { + "X": block.var('x'), + "Scale": block.var('scale'), + "Bias": block.var('bias'), + "Mean": block.var('mean'), + "Variance": block.var('variance'), + } + attrs = { + "epsilon": epsilon, + "is_test": False, + "data_layout": data_layout, + "use_mkldnn": self.use_mkldnn, + "fuse_with_relu": self.fuse_with_relu, + "use_global_stats": self.use_global_stats, + } + if self.use_momentum_variable: + inputs['MomentumTensor'] = block.var('momentum_var') + else: + attrs['momentum'] = momentum + + outputs = { + "Y": block.var('y'), + "MeanOut": block.var('mean'), # share memory + "VarianceOut": block.var('variance'), # share memory + "SavedMean": block.var('saved_mean'), + "SavedVariance": block.var('saved_variance'), + } + block.create_var(name="reserve_space", dtype='float32') + outputs["ReserveSpace"] = block.var('reserve_space') + bn_op = block.append_op( + type="batch_norm", + inputs=inputs, + outputs=outputs, + attrs=attrs, + ) + block.create_var(name='y@GRAD', dtype='float32', shape=y.shape) + + # generate backward op_desc + grad_op_desc_list, op_grad_to_var = core.get_grad_op_desc( + bn_op.desc, self.no_grad_set, [] + ) + grad_op_desc = grad_op_desc_list[0] + new_op_desc = block.desc.append_op() + new_op_desc.copy_from(grad_op_desc) + for var_name in grad_op_desc.output_arg_names(): + block.desc.var(var_name.encode("ascii")) + grad_op_desc.infer_var_type(block.desc) + grad_op_desc.infer_shape(block.desc) + for arg in grad_op_desc.output_arg_names(): + grad_var = block.desc.find_var(arg.encode("ascii")) + grad_var.set_dtype(core.VarDesc.VarType.FP32) + + program._sync_with_cpp() + + exe = base.Executor(place) + out = exe.run( + program, + feed={ + name: var_dict[name] + for name in [ + 'x', + 'scale', + 'bias', + 'mean', + 'variance', + 'y@GRAD', + 'momentum_var', + ] + }, + fetch_list=self.fetch_list, + ) + + for id, name in enumerate(self.fetch_list): + if name == 'variance': + self.__assert_close( + var_dict[name], out[id], name, atol=1e-3 + ) + continue + self.__assert_close(var_dict[name], out[id], name) + print("op test forward passed: ", str(place), data_layout) + + places = [core.CPUPlace()] + + if core.is_compiled_with_cuda(): + places.append(core.CUDAPlace(0)) + + for place in places: + for data_format in self.data_formats: + test_with_place(place, data_format, [2, 3, 4, 5]) + + def init_kernel_type(self): + pass + + +class TestBatchNormOpTrainingCase1(TestBatchNormOpTraining): + def init_test_case(self): + self.use_global_stats = False + self.no_grad_set = {'scale@GRAD', 'bias@GRAD'} + self.fetch_list = ['y', 'mean', 'variance', 'x@GRAD'] + + +class TestBatchNormOpTrainingCase2(TestBatchNormOpTraining): + def init_test_case(self): + self.use_global_stats = False + self.no_grad_set = set() + self.fetch_list = [ + 'y', + 'mean', + 'variance', + 'saved_mean', + 'saved_variance', + 'x@GRAD', + 'scale@GRAD', + 'bias@GRAD', + ] + os.environ['FLAGS_cudnn_batchnorm_spatial_persistent'] = "1" + + +class TestBatchNormOpTrainingCase3(TestBatchNormOpTraining): + def init_test_case(self): + self.use_global_stats = False + self.no_grad_set = {'x@GRAD'} + self.fetch_list = ['y', 'mean', 'variance', 'scale@GRAD', 'bias@GRAD'] + + +class TestBatchNormOpTrainingMomentumVariable(TestBatchNormOpTraining): + def init_test_case(self): + self.use_momentum_variable = True + self.use_global_stats = False + self.no_grad_set = set() + self.fetch_list = [ + 'y', + 'mean', + 'variance', + 'saved_mean', + 'saved_variance', + 'x@GRAD', + 'scale@GRAD', + 'bias@GRAD', + ] + + +class TestBatchNormOpFreezeStatsTraining(TestBatchNormOpTraining): + def init_test_case(self): + self.use_global_stats = True + self.no_grad_set = set() + self.fetch_list = [ + 'y', + 'mean', + 'variance', + 'x@GRAD', + 'scale@GRAD', + 'bias@GRAD', + ] + + def reference_grad(self, x, y_grad, scale, mean, var, epsilon, data_format): + if data_format == "NCHW": + x = np.transpose(x, (0, 2, 3, 1)) + y_grad = np.transpose(y_grad, (0, 2, 3, 1)) + + x_grad = scale * y_grad / np.sqrt(var + epsilon) + grad_scale = np.sum( + y_grad * (x - mean) / np.sqrt(var + epsilon), axis=(0, 1, 2) + ) + grad_offset = np.sum(y_grad, axis=(0, 1, 2)) + + # transfer back to N, C, H, W + if data_format == "NCHW": + x_grad = np.transpose(x_grad, (0, 3, 1, 2)) + x = np.transpose(x, (0, 3, 1, 2)) + y_grad = np.transpose(y_grad, (0, 3, 1, 2)) + + return x_grad, grad_scale, grad_offset + + def ref_forward_backward( + self, + x, + y_grad, + scale, + bias, + mean, + variance, + epsilon, + momentum, + shape, + data_layout, + ): + if data_layout != "NCHW" and data_layout != "NHWC": + raise ValueError("Unknown data order.") + + if data_layout == "NCHW": + x = np.transpose(x, (0, 2, 3, 1)) + + # run normalizaton + normalized = (x - mean) / np.sqrt(variance + epsilon) + y = normalized * scale + bias + + # transfer back to N, C, H, W + if data_layout == "NCHW": + x = np.transpose(x, (0, 3, 1, 2)) + y = np.transpose(y, (0, 3, 1, 2)) + + mean_out = mean + variance_out = variance + saved_variance = 1.0 / np.sqrt(variance + epsilon) + # run backward + x_grad, scale_grad, bias_grad = self.reference_grad( + x, y_grad, scale, mean, variance, epsilon, data_layout + ) + + return ( + y, + mean_out, + variance_out, + mean, + saved_variance, + x_grad, + scale_grad, + bias_grad, + ) + + +class TestBatchNormOpFreezeStatsAndScaleBiasTraining( + TestBatchNormOpFreezeStatsTraining +): + def init_test_case(self): + self.use_global_stats = True + self.no_grad_set = {'scale@GRAD', 'bias@GRAD'} + self.fetch_list = ['y', 'mean', 'variance', 'x@GRAD'] + + +if __name__ == '__main__': + paddle.enable_static() + unittest.main() diff --git a/test/deprecated/legacy_test/test_bilinear_tensor_product_op.py b/test/deprecated/legacy_test/test_bilinear_tensor_product_op_deprecated.py similarity index 64% rename from test/deprecated/legacy_test/test_bilinear_tensor_product_op.py rename to test/deprecated/legacy_test/test_bilinear_tensor_product_op_deprecated.py index 20dcc132e80d66..dee0412af3bc56 100644 --- a/test/deprecated/legacy_test/test_bilinear_tensor_product_op.py +++ b/test/deprecated/legacy_test/test_bilinear_tensor_product_op_deprecated.py @@ -15,7 +15,7 @@ import unittest import numpy as np -from op_test import OpTest, paddle_static_guard +from op_test import paddle_static_guard import paddle from paddle import base @@ -52,37 +52,5 @@ def test_errors(self): ) -class TestBilinearTensorProductOp(OpTest): - def setUp(self): - self.op_type = "bilinear_tensor_product" - self.python_api = paddle.nn.functional.bilinear - batch_size = 6 - size0 = 5 - size1 = 4 - size2 = 5 - 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) - bias = np.random.random((1, size2)).astype(dtype) - output = np.zeros((batch_size, size2)).astype(dtype) - for i in range(size2): - w_i = w[i, :, :] - output[:, i] = np.sum(np.matmul(a, w_i) * b, axis=1) - self.inputs = { - 'X': a, - 'Y': b, - 'Weight': w, - 'Bias': bias, - } - self.outputs = {'Out': output + bias} - - def test_check_output(self): - self.check_output() - - def test_check_grad_normal(self): - self.check_grad(['X', 'Y', 'Weight', 'Bias'], 'Out') - - if __name__ == "__main__": unittest.main() diff --git a/test/deprecated/legacy_test/test_communicator_geo.py b/test/deprecated/legacy_test/test_communicator_geo_deprecated.py similarity index 100% rename from test/deprecated/legacy_test/test_communicator_geo.py rename to test/deprecated/legacy_test/test_communicator_geo_deprecated.py diff --git a/test/deprecated/legacy_test/test_compiled_program.py b/test/deprecated/legacy_test/test_compiled_program.py index 1b6f3698afa4d8..66b9039212ff16 100644 --- a/test/deprecated/legacy_test/test_compiled_program.py +++ b/test/deprecated/legacy_test/test_compiled_program.py @@ -76,47 +76,5 @@ def test_compiled_program_base(self): np.testing.assert_array_equal(float(loss_data), self.loss) -class TestCompiledProgramError(unittest.TestCase): - def test_program_or_graph_error(self): - self.assertRaises(TypeError, base.CompiledProgram, "program") - - def build_simple_model(self): - img = paddle.static.data( - name='image', shape=[-1, 1, 28, 28], dtype='float32' - ) - label = paddle.static.data(name='label', shape=[-1, 1], dtype='int64') - prediction = paddle.static.nn.fc(x=img, size=10, activation='softmax') - loss = paddle.nn.functional.cross_entropy( - input=prediction, label=label, reduction='none', use_softmax=False - ) - avg_loss = paddle.mean(loss) - - def compile_program(self): - with base.program_guard(base.Program()): - # build model - self.build_simple_model() - # compile program - 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 - - def test_compile_scope_error(self): - compiled_program, _, place = self.compile_program() - new_scope = core.Scope() - with self.assertRaises(ValueError): - compiled_program._compile(new_scope, place) - - def test_compile_place_error(self): - # need create different place - if core.is_compiled_with_cuda(): - compiled_program, scope, _ = self.compile_program() - new_place = base.CUDAPlace(0) - with self.assertRaises(ValueError): - compiled_program._compile(scope, new_place) - - if __name__ == '__main__': unittest.main() diff --git a/test/deprecated/legacy_test/test_compiled_program_deprecated.py b/test/deprecated/legacy_test/test_compiled_program_deprecated.py new file mode 100644 index 00000000000000..597cf6af8c7fd2 --- /dev/null +++ b/test/deprecated/legacy_test/test_compiled_program_deprecated.py @@ -0,0 +1,70 @@ +# copyright (c) 2020 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 + +sys.path.append("../../legacy_test") + +import paddle +from paddle import base +from paddle.base import core + +paddle.enable_static() + + +class TestCompiledProgramError(unittest.TestCase): + def test_program_or_graph_error(self): + self.assertRaises(TypeError, base.CompiledProgram, "program") + + def build_simple_model(self): + img = paddle.static.data( + name='image', shape=[-1, 1, 28, 28], dtype='float32' + ) + label = paddle.static.data(name='label', shape=[-1, 1], dtype='int64') + prediction = paddle.static.nn.fc(x=img, size=10, activation='softmax') + loss = paddle.nn.functional.cross_entropy( + input=prediction, label=label, reduction='none', use_softmax=False + ) + avg_loss = paddle.mean(loss) + + def compile_program(self): + with base.program_guard(base.Program()): + # build model + self.build_simple_model() + # compile program + 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 + + def test_compile_scope_error(self): + compiled_program, _, place = self.compile_program() + new_scope = core.Scope() + with self.assertRaises(ValueError): + compiled_program._compile(new_scope, place) + + def test_compile_place_error(self): + # need create different place + if core.is_compiled_with_cuda(): + compiled_program, scope, _ = self.compile_program() + new_place = base.CUDAPlace(0) + with self.assertRaises(ValueError): + compiled_program._compile(scope, new_place) + + +if __name__ == '__main__': + unittest.main() diff --git a/test/deprecated/legacy_test/test_conditional_block.py b/test/deprecated/legacy_test/test_conditional_block_deprecated.py similarity index 100% rename from test/deprecated/legacy_test/test_conditional_block.py rename to test/deprecated/legacy_test/test_conditional_block_deprecated.py diff --git a/test/deprecated/legacy_test/test_conv3d_transpose_part2_op.py b/test/deprecated/legacy_test/test_conv3d_transpose_part2_op_deprecated.py similarity index 65% rename from test/deprecated/legacy_test/test_conv3d_transpose_part2_op.py rename to test/deprecated/legacy_test/test_conv3d_transpose_part2_op_deprecated.py index 571c961ff4190b..7fcc24b5d67e7f 100644 --- a/test/deprecated/legacy_test/test_conv3d_transpose_part2_op.py +++ b/test/deprecated/legacy_test/test_conv3d_transpose_part2_op_deprecated.py @@ -18,95 +18,12 @@ import numpy as np sys.path.append("../../legacy_test") -from test_conv3d_transpose_op import ( - TestConv3DTransposeOp, - create_test_cudnn_bf16_class, - create_test_cudnn_fp16_class, -) import paddle from paddle import base from paddle.base import core -class TestWithSymmetricPad_NHWC(TestConv3DTransposeOp): - def init_test_case(self): - self.pad = [1, 1, 1] - self.stride = [1, 1, 1] - self.dilations = [1, 1, 1] - self.groups = 1 - self.input_size = [2, 5, 5, 5, 3] # NDHWC - f_c = self.input_size[-1] - self.filter_size = [f_c, 6, 3, 3, 3] - self.data_format = 'NHWC' - - -class TestWithAsymmetricPad_NHWC(TestConv3DTransposeOp): - def init_test_case(self): - self.pad = [1, 0, 1, 0, 1, 2] - self.stride = [1, 1, 1] - self.dilations = [1, 1, 1] - self.groups = 1 - self.input_size = [2, 5, 5, 5, 3] # NDHWC - f_c = self.input_size[-1] - self.filter_size = [f_c, 6, 3, 3, 3] - self.data_format = 'NHWC' - - -class TestWithGroups_NHWC(TestConv3DTransposeOp): - def init_test_case(self): - self.check_no_filter = True - self.pad = [1, 1, 1] - self.stride = [1, 1, 1] - self.dilations = [1, 1, 1] - self.groups = 2 - self.input_size = [2, 5, 5, 5, 4] # NDHWC - f_c = self.input_size[-1] - self.filter_size = [f_c, 3, 3, 3, 3] - self.data_format = 'NHWC' - - -class TestWithStride_NHWC(TestConv3DTransposeOp): - def init_test_case(self): - self.pad = [1, 1, 1] - self.stride = [2, 2, 2] - self.dilations = [1, 1, 1] - self.groups = 1 - self.input_size = [2, 5, 5, 5, 3] # NCDHW - f_c = self.input_size[-1] - self.filter_size = [f_c, 6, 3, 3, 3] - self.data_format = 'NHWC' - - -class TestWithDilation_NHWC(TestConv3DTransposeOp): - def init_test_case(self): - self.check_no_input = True - self.pad = [1, 1, 1] - self.stride = [1, 1, 1] - self.dilations = [2, 2, 2] - self.groups = 1 - self.input_size = [2, 5, 5, 5, 3] # NCDHW - f_c = self.input_size[-1] - self.filter_size = [f_c, 6, 3, 3, 3] - self.data_format = 'NHWC' - - -# ----------------Conv3DTransposeCUDNN fp16---------------- -create_test_cudnn_fp16_class(TestWithSymmetricPad_NHWC) -create_test_cudnn_fp16_class(TestWithAsymmetricPad_NHWC) -create_test_cudnn_fp16_class(TestWithGroups_NHWC) -create_test_cudnn_fp16_class(TestWithStride_NHWC) -create_test_cudnn_fp16_class(TestWithDilation_NHWC) - - -# ----------------Conv3DTransposeCUDNN bf16---------------- -create_test_cudnn_bf16_class(TestWithSymmetricPad_NHWC) -create_test_cudnn_bf16_class(TestWithAsymmetricPad_NHWC) -create_test_cudnn_bf16_class(TestWithGroups_NHWC) -create_test_cudnn_bf16_class(TestWithStride_NHWC) -create_test_cudnn_bf16_class(TestWithDilation_NHWC) - - class TestConv3DTransposeAPI(unittest.TestCase): def test_case1(self): data1 = paddle.static.data( diff --git a/test/deprecated/legacy_test/test_cost_model.py b/test/deprecated/legacy_test/test_cost_model.py index 997a5c0c6c47b7..77220b5a0cfba3 100644 --- a/test/deprecated/legacy_test/test_cost_model.py +++ b/test/deprecated/legacy_test/test_cost_model.py @@ -33,26 +33,6 @@ def test_profiler_measure_empty_program(self): ) self.assertEqual(cost_data.get_whole_time_ms(), 0) - def test_profiler_measure_program(self): - main_program = paddle.static.Program() - startup_program = paddle.static.Program() - with paddle.static.program_guard(main_program, startup_program): - # TODO(zhhsplendid): support paddle.static.data, which is uninitialized data - data = paddle.ones(name='X', shape=[16, 100], dtype='float32') - hidden = paddle.static.nn.fc(data, 10) - loss = paddle.mean(hidden) - cost_model = core.CostModel() - cost_data = cost_model.profile_measure( - main_program, startup_program, device, ["time"] - ) - fc_op_time = cost_data.get_op_time_ms(0) - mean_op_time = cost_data.get_op_time_ms(1) - self.assertGreater(fc_op_time, 0) - self.assertGreater(mean_op_time, 0) - self.assertGreaterEqual( - cost_data.get_whole_time_ms(), fc_op_time + mean_op_time - ) - def test_static_op_benchmark_cost_model(self): op_name = "abs" cost_model = CostModel() diff --git a/test/deprecated/legacy_test/test_cost_model_deprecated.py b/test/deprecated/legacy_test/test_cost_model_deprecated.py new file mode 100644 index 00000000000000..b86b286ad47dbe --- /dev/null +++ b/test/deprecated/legacy_test/test_cost_model_deprecated.py @@ -0,0 +1,48 @@ +# 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 +from paddle.base import core + +paddle.enable_static() + +device = "gpu" if core.is_compiled_with_cuda() else "cpu" + + +class TestCostModel(unittest.TestCase): + def test_profiler_measure_program(self): + main_program = paddle.static.Program() + startup_program = paddle.static.Program() + with paddle.static.program_guard(main_program, startup_program): + # TODO(zhhsplendid): support paddle.static.data, which is uninitialized data + data = paddle.ones(name='X', shape=[16, 100], dtype='float32') + hidden = paddle.static.nn.fc(data, 10) + loss = paddle.mean(hidden) + cost_model = core.CostModel() + cost_data = cost_model.profile_measure( + main_program, startup_program, device, ["time"] + ) + fc_op_time = cost_data.get_op_time_ms(0) + mean_op_time = cost_data.get_op_time_ms(1) + self.assertGreater(fc_op_time, 0) + self.assertGreater(mean_op_time, 0) + self.assertGreaterEqual( + cost_data.get_whole_time_ms(), fc_op_time + mean_op_time + ) + + +if __name__ == '__main__': + unittest.main() diff --git a/test/deprecated/legacy_test/test_data_norm_op_deprecated.py b/test/deprecated/legacy_test/test_data_norm_op_deprecated.py new file mode 100644 index 00000000000000..4019ab0c0bf40c --- /dev/null +++ b/test/deprecated/legacy_test/test_data_norm_op_deprecated.py @@ -0,0 +1,60 @@ +# 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. +"""This is unit test of Test data_norm Op.""" + +import unittest + +import numpy as np + +import paddle +from paddle import base +from paddle.base import Program, program_guard + +paddle.enable_static() + + +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, base.data_norm, x2) + paddle.static.nn.data_norm( + input=x2, param_attr={}, enable_scale_and_shift=True + ) + + # Test input with dimension 1 + paddle.enable_static() + x3 = paddle.static.data("", shape=[0], dtype="float32") + self.assertRaises(ValueError, paddle.static.nn.data_norm, x3) + + # The size of input in data_norm should not be 0. + 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 = 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, + feed={'x': np.ones([0, 3]).astype('float32')}, + ) + + self.assertRaises(ValueError, test_0_size) + + +if __name__ == '__main__': + unittest.main() diff --git a/test/deprecated/legacy_test/test_dataloader_early_reset.py b/test/deprecated/legacy_test/test_dataloader_early_reset_deprecated.py similarity index 99% rename from test/deprecated/legacy_test/test_dataloader_early_reset.py rename to test/deprecated/legacy_test/test_dataloader_early_reset_deprecated.py index 1c826eb9cb89ae..65c1488f66ae0d 100644 --- a/test/deprecated/legacy_test/test_dataloader_early_reset.py +++ b/test/deprecated/legacy_test/test_dataloader_early_reset_deprecated.py @@ -19,6 +19,8 @@ import paddle from paddle import base +paddle.enable_static() + def infinite_reader(): num = 0 diff --git a/test/deprecated/legacy_test/test_dataloader_keep_order.py b/test/deprecated/legacy_test/test_dataloader_keep_order_deprecated.py similarity index 99% rename from test/deprecated/legacy_test/test_dataloader_keep_order.py rename to test/deprecated/legacy_test/test_dataloader_keep_order_deprecated.py index a37e1b49397709..8c05c6a97344c5 100644 --- a/test/deprecated/legacy_test/test_dataloader_keep_order.py +++ b/test/deprecated/legacy_test/test_dataloader_keep_order_deprecated.py @@ -20,6 +20,8 @@ import paddle from paddle import base +paddle.enable_static() + def create_reader(shape, batch_number): def __impl__(): diff --git a/test/deprecated/legacy_test/test_dataloader_unkeep_order.py b/test/deprecated/legacy_test/test_dataloader_unkeep_order_deprecated.py similarity index 99% rename from test/deprecated/legacy_test/test_dataloader_unkeep_order.py rename to test/deprecated/legacy_test/test_dataloader_unkeep_order_deprecated.py index 17e5257dffc012..acc272b766f895 100644 --- a/test/deprecated/legacy_test/test_dataloader_unkeep_order.py +++ b/test/deprecated/legacy_test/test_dataloader_unkeep_order_deprecated.py @@ -21,6 +21,8 @@ from paddle import base from paddle.base.reader import keep_data_loader_order +paddle.enable_static() + keep_data_loader_order(False) diff --git a/test/deprecated/legacy_test/test_dataset.py b/test/deprecated/legacy_test/test_dataset.py index fd4141c1c8b64d..6a005c1f285761 100644 --- a/test/deprecated/legacy_test/test_dataset.py +++ b/test/deprecated/legacy_test/test_dataset.py @@ -923,148 +923,6 @@ def setUp(self): self.drop_last = False -class TestDatasetWithFetchHandler(unittest.TestCase): - """ - Test Dataset With Fetch Handler. TestCases. - """ - - def net(self): - """ - Test Dataset With Fetch Handler. TestCases. - """ - slots = ["slot1", "slot2", "slot3", "slot4"] - slots_vars = [] - poolings = [] - for slot in slots: - data = paddle.static.data( - name=slot, shape=[-1, 1], dtype="int64", lod_level=1 - ) - var = paddle.cast(x=data, dtype='float32') - pool = paddle.static.nn.sequence_lod.sequence_pool( - input=var, pool_type='AVERAGE' - ) - - slots_vars.append(data) - poolings.append(pool) - - concated = paddle.concat(poolings, axis=1) - fc = paddle.static.nn.fc(x=concated, activation='tanh', size=32) - return slots_vars, fc - - def get_dataset(self, inputs, files): - """ - Test Dataset With Fetch Handler. TestCases. - - Args: - inputs(list): inputs of get_dataset - files(list): files of get_dataset - """ - dataset = paddle.distributed.QueueDataset() - dataset.init( - batch_size=32, thread_num=2, pipe_command="cat", use_var=inputs - ) - dataset.set_filelist(files) - return dataset - - def setUp(self): - """ - Test Dataset With Fetch Handler. TestCases. - """ - self.temp_dir = tempfile.TemporaryDirectory() - self.filename1 = os.path.join( - self.temp_dir.name, "test_queue_dataset_run_a.txt" - ) - self.filename2 = os.path.join( - self.temp_dir.name, "test_queue_dataset_run_b.txt" - ) - - with open(self.filename1, "w") as f: - data = "1 1 2 3 3 4 5 5 5 5 1 1\n" - data += "1 2 2 3 4 4 6 6 6 6 1 2\n" - data += "1 3 2 3 5 4 7 7 7 7 1 3\n" - f.write(data) - with open(self.filename2, "w") as f: - data = "1 4 2 3 3 4 5 5 5 5 1 4\n" - data += "1 5 2 3 4 4 6 6 6 6 1 5\n" - data += "1 6 2 3 5 4 7 7 7 7 1 6\n" - data += "1 7 2 3 6 4 8 8 8 8 1 7\n" - f.write(data) - - def tearDown(self): - """ - Test Dataset With Fetch Handler. TestCases. - """ - self.temp_dir.cleanup() - - def test_dataset_none(self): - """ - Test Dataset With Fetch Handler. TestCases. - """ - slots_vars, out = self.net() - files = [self.filename1, self.filename2] - dataset = self.get_dataset(slots_vars, files) - - exe = base.Executor(base.CPUPlace()) - exe.run(base.default_startup_program()) - - # test dataset->None - try: - 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: - error_msg = "dataset is need and should be initialized" - self.assertEqual(error_msg, str(e)) - except Exception as e: - self.assertTrue(False) - - def test_infer_from_dataset(self): - """ - Test Dataset With Fetch Handler. TestCases. - """ - slots_vars, out = self.net() - files = [self.filename1, self.filename2] - dataset = self.get_dataset(slots_vars, files) - - exe = base.Executor(base.CPUPlace()) - exe.run(base.default_startup_program()) - - try: - 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: - self.assertTrue(False) - - def test_fetch_handler(self): - """ - Test Dataset With Fetch Handler. TestCases. - """ - slots_vars, out = self.net() - files = [self.filename1, self.filename2] - dataset = self.get_dataset(slots_vars, files) - - exe = base.Executor(base.CPUPlace()) - exe.run(base.default_startup_program()) - - fh = base.executor.FetchHandler(out.name) - fh.help() - - try: - exe.train_from_dataset( - program=base.default_main_program(), - dataset=dataset, - fetch_handler=fh, - ) - except ImportError as e: - print("warning: we skip trainer_desc_pb2 import problem in windows") - except RuntimeError as e: - error_msg = "dataset is need and should be initialized" - self.assertEqual(error_msg, str(e)) - except Exception as e: - self.assertTrue(False) - - class TestDataset2(unittest.TestCase): """TestCases for Dataset.""" diff --git a/test/deprecated/legacy_test/test_dataset_deprecated.py b/test/deprecated/legacy_test/test_dataset_deprecated.py new file mode 100644 index 00000000000000..f3af35297e2845 --- /dev/null +++ b/test/deprecated/legacy_test/test_dataset_deprecated.py @@ -0,0 +1,172 @@ +# Copyright (c) 2018 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. +""" +TestCases for Dataset, +including create, config, run, etc. +""" + +import os +import tempfile +import unittest + +import paddle +from paddle import base + +paddle.enable_static() + + +class TestDatasetWithFetchHandler(unittest.TestCase): + """ + Test Dataset With Fetch Handler. TestCases. + """ + + def net(self): + """ + Test Dataset With Fetch Handler. TestCases. + """ + slots = ["slot1", "slot2", "slot3", "slot4"] + slots_vars = [] + poolings = [] + for slot in slots: + data = paddle.static.data( + name=slot, shape=[-1, 1], dtype="int64", lod_level=1 + ) + var = paddle.cast(x=data, dtype='float32') + pool = paddle.static.nn.sequence_lod.sequence_pool( + input=var, pool_type='AVERAGE' + ) + + slots_vars.append(data) + poolings.append(pool) + + concated = paddle.concat(poolings, axis=1) + fc = paddle.static.nn.fc(x=concated, activation='tanh', size=32) + return slots_vars, fc + + def get_dataset(self, inputs, files): + """ + Test Dataset With Fetch Handler. TestCases. + + Args: + inputs(list): inputs of get_dataset + files(list): files of get_dataset + """ + dataset = paddle.distributed.QueueDataset() + dataset.init( + batch_size=32, thread_num=2, pipe_command="cat", use_var=inputs + ) + dataset.set_filelist(files) + return dataset + + def setUp(self): + """ + Test Dataset With Fetch Handler. TestCases. + """ + self.temp_dir = tempfile.TemporaryDirectory() + self.filename1 = os.path.join( + self.temp_dir.name, "test_queue_dataset_run_a.txt" + ) + self.filename2 = os.path.join( + self.temp_dir.name, "test_queue_dataset_run_b.txt" + ) + + with open(self.filename1, "w") as f: + data = "1 1 2 3 3 4 5 5 5 5 1 1\n" + data += "1 2 2 3 4 4 6 6 6 6 1 2\n" + data += "1 3 2 3 5 4 7 7 7 7 1 3\n" + f.write(data) + with open(self.filename2, "w") as f: + data = "1 4 2 3 3 4 5 5 5 5 1 4\n" + data += "1 5 2 3 4 4 6 6 6 6 1 5\n" + data += "1 6 2 3 5 4 7 7 7 7 1 6\n" + data += "1 7 2 3 6 4 8 8 8 8 1 7\n" + f.write(data) + + def tearDown(self): + """ + Test Dataset With Fetch Handler. TestCases. + """ + self.temp_dir.cleanup() + + def test_dataset_none(self): + """ + Test Dataset With Fetch Handler. TestCases. + """ + slots_vars, out = self.net() + files = [self.filename1, self.filename2] + dataset = self.get_dataset(slots_vars, files) + + exe = base.Executor(base.CPUPlace()) + exe.run(base.default_startup_program()) + + # test dataset->None + try: + 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: + error_msg = "dataset is need and should be initialized" + self.assertEqual(error_msg, str(e)) + except Exception as e: + self.assertTrue(False) + + def test_infer_from_dataset(self): + """ + Test Dataset With Fetch Handler. TestCases. + """ + slots_vars, out = self.net() + files = [self.filename1, self.filename2] + dataset = self.get_dataset(slots_vars, files) + + exe = base.Executor(base.CPUPlace()) + exe.run(base.default_startup_program()) + + try: + 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: + self.assertTrue(False) + + def test_fetch_handler(self): + """ + Test Dataset With Fetch Handler. TestCases. + """ + slots_vars, out = self.net() + files = [self.filename1, self.filename2] + dataset = self.get_dataset(slots_vars, files) + + exe = base.Executor(base.CPUPlace()) + exe.run(base.default_startup_program()) + + fh = base.executor.FetchHandler(out.name) + fh.help() + + try: + exe.train_from_dataset( + program=base.default_main_program(), + dataset=dataset, + fetch_handler=fh, + ) + except ImportError as e: + print("warning: we skip trainer_desc_pb2 import problem in windows") + except RuntimeError as e: + error_msg = "dataset is need and should be initialized" + self.assertEqual(error_msg, str(e)) + except Exception as e: + self.assertTrue(False) + + +if __name__ == '__main__': + unittest.main() diff --git a/test/deprecated/legacy_test/test_decoupled_py_reader.py b/test/deprecated/legacy_test/test_decoupled_py_reader_deprecated.py similarity index 99% rename from test/deprecated/legacy_test/test_decoupled_py_reader.py rename to test/deprecated/legacy_test/test_decoupled_py_reader_deprecated.py index a28b2584a5ff60..f9c75dc7dfd595 100644 --- a/test/deprecated/legacy_test/test_decoupled_py_reader.py +++ b/test/deprecated/legacy_test/test_decoupled_py_reader_deprecated.py @@ -20,6 +20,8 @@ import paddle from paddle import base +paddle.enable_static() + EPOCH_NUM = 5 BATCH_SIZE = 16 BATCH_NUM = 10 diff --git a/test/deprecated/legacy_test/test_deform_conv2d.py b/test/deprecated/legacy_test/test_deform_conv2d_deprecated.py similarity index 57% rename from test/deprecated/legacy_test/test_deform_conv2d.py rename to test/deprecated/legacy_test/test_deform_conv2d_deprecated.py index 23208363b5ff95..d8ad41359adfdc 100644 --- a/test/deprecated/legacy_test/test_deform_conv2d.py +++ b/test/deprecated/legacy_test/test_deform_conv2d_deprecated.py @@ -19,213 +19,6 @@ import paddle import paddle.nn.initializer as I -from paddle.pir_utils import test_with_pir_api - - -class TestDeformConv2D(TestCase): - batch_size = 4 - spatial_shape = (5, 5) - dtype = "float32" - - def setUp(self): - self.in_channels = 2 - self.out_channels = 5 - self.kernel_size = [3, 3] - self.padding = [0, 0] - self.stride = [1, 1] - self.dilation = [1, 1] - self.deformable_groups = 1 - self.groups = 1 - self.no_bias = True - - def prepare(self): - np.random.seed(1) - paddle.seed(1) - if isinstance(self.kernel_size, int): - filter_shape = (self.kernel_size,) * 2 - else: - filter_shape = tuple(self.kernel_size) - self.filter_shape = filter_shape - - self.weight = np.random.uniform( - -1, - 1, - (self.out_channels, self.in_channels // self.groups) + filter_shape, - ).astype(self.dtype) - if not self.no_bias: - self.bias = np.random.uniform(-1, 1, (self.out_channels,)).astype( - self.dtype - ) - - def out_size( - in_size, pad_size, dilation_size, kernel_size, stride_size - ): - return ( - in_size + 2 * pad_size - (dilation_size * (kernel_size - 1) + 1) - ) / stride_size + 1 - - out_h = int( - out_size( - self.spatial_shape[0], - self.padding[0], - self.dilation[0], - self.kernel_size[0], - self.stride[0], - ) - ) - out_w = int( - out_size( - self.spatial_shape[1], - self.padding[1], - self.dilation[1], - self.kernel_size[1], - self.stride[1], - ) - ) - out_shape = (out_h, out_w) - - self.input_shape = ( - self.batch_size, - self.in_channels, - ) + self.spatial_shape - - self.offset_shape = ( - self.batch_size, - self.deformable_groups * 2 * filter_shape[0] * filter_shape[1], - ) + out_shape - - self.mask_shape = ( - self.batch_size, - self.deformable_groups * filter_shape[0] * filter_shape[1], - ) + out_shape - - self.input = np.random.uniform(-1, 1, self.input_shape).astype( - self.dtype - ) - - self.offset = np.random.uniform(-1, 1, self.offset_shape).astype( - self.dtype - ) - - self.mask = np.random.uniform(-1, 1, self.mask_shape).astype(self.dtype) - - def static_graph_case_dcn(self): - main = paddle.static.Program() - start = paddle.static.Program() - paddle.enable_static() - with paddle.static.program_guard(main, start): - x = paddle.static.data( - "input", (-1, self.in_channels, -1, -1), dtype=self.dtype - ) - offset = paddle.static.data( - "offset", - ( - -1, - self.deformable_groups - * 2 - * self.filter_shape[0] - * self.filter_shape[1], - -1, - -1, - ), - dtype=self.dtype, - ) - mask = paddle.static.data( - "mask", - ( - -1, - self.deformable_groups - * self.filter_shape[0] - * self.filter_shape[1], - -1, - -1, - ), - dtype=self.dtype, - ) - - y_v1 = paddle.vision.ops.DeformConv2D( - in_channels=self.in_channels, - out_channels=self.out_channels, - kernel_size=self.filter_shape, - stride=self.stride, - padding=self.padding, - dilation=self.dilation, - groups=self.groups, - deformable_groups=self.deformable_groups, - weight_attr=I.Assign(self.weight), - bias_attr=False if self.no_bias else I.Assign(self.bias), - )(x, offset, None) - - y_v2 = paddle.vision.ops.DeformConv2D( - in_channels=self.in_channels, - out_channels=self.out_channels, - kernel_size=self.filter_shape, - stride=self.stride, - padding=self.padding, - dilation=self.dilation, - groups=self.groups, - deformable_groups=self.deformable_groups, - weight_attr=I.Assign(self.weight), - bias_attr=False if self.no_bias else I.Assign(self.bias), - )(x, offset, mask) - - exe = paddle.static.Executor(self.place) - exe.run(start) - out_v1, out_v2 = exe.run( - main, - feed={ - "input": self.input, - "offset": self.offset, - "mask": self.mask, - }, - fetch_list=[y_v1, y_v2], - ) - return out_v1, out_v2 - - def dygraph_case_dcn(self): - paddle.disable_static() - x = paddle.to_tensor(self.input) - offset = paddle.to_tensor(self.offset) - mask = paddle.to_tensor(self.mask) - - bias = None if self.no_bias else paddle.to_tensor(self.bias) - - deform_conv2d = paddle.vision.ops.DeformConv2D( - in_channels=self.in_channels, - out_channels=self.out_channels, - kernel_size=self.kernel_size, - stride=self.stride, - padding=self.padding, - dilation=self.dilation, - deformable_groups=self.deformable_groups, - groups=self.groups, - weight_attr=I.Assign(self.weight), - bias_attr=False if self.no_bias else I.Assign(self.bias), - ) - - y_v1 = deform_conv2d(x, offset) - y_v2 = deform_conv2d(x, offset, mask) - - out_v1 = y_v1.numpy() - out_v2 = y_v2.numpy() - - return out_v1, out_v2 - - @test_with_pir_api - def _test_identity(self): - self.prepare() - static_dcn_v1, static_dcn_v2 = self.static_graph_case_dcn() - dy_dcn_v1, dy_dcn_v2 = self.dygraph_case_dcn() - np.testing.assert_array_almost_equal(static_dcn_v1, dy_dcn_v1) - np.testing.assert_array_almost_equal(static_dcn_v2, dy_dcn_v2) - - def test_identity(self): - self.place = paddle.CPUPlace() - self._test_identity() - - if paddle.is_compiled_with_cuda(): - self.place = paddle.CUDAPlace(0) - self._test_identity() class TestDeformConv2DFunctional(TestCase): @@ -536,98 +329,6 @@ def test_identity(self): self._test_identity() -# testcases for DeformConv2D -class TestDeformConv2DWithPadding(TestDeformConv2D): - def setUp(self): - self.in_channels = 3 - self.out_channels = 5 - self.kernel_size = [3, 3] - self.padding = [2, 2] - self.stride = [1, 1] - self.dilation = [1, 1] - self.deformable_groups = 1 - self.groups = 1 - self.no_bias = True - - -class TestDeformConv2DWithBias(TestDeformConv2D): - def setUp(self): - self.in_channels = 3 - self.out_channels = 5 - self.kernel_size = [3, 3] - self.padding = [2, 2] - self.stride = [1, 1] - self.dilation = [1, 1] - self.deformable_groups = 1 - self.groups = 1 - self.no_bias = False - - -class TestDeformConv2DWithAsynPadding(TestDeformConv2D): - def setUp(self): - self.in_channels = 3 - self.out_channels = 5 - self.kernel_size = [3, 3] - self.padding = [1, 2] - self.stride = [1, 1] - self.dilation = [1, 1] - self.deformable_groups = 1 - self.groups = 1 - self.no_bias = False - - -class TestDeformConv2DWithDilation(TestDeformConv2D): - def setUp(self): - self.in_channels = 3 - self.out_channels = 5 - self.kernel_size = [3, 3] - self.padding = [1, 1] - self.stride = [1, 1] - self.dilation = [3, 3] - self.deformable_groups = 1 - self.groups = 1 - self.no_bias = False - - -class TestDeformConv2DWithStride(TestDeformConv2D): - def setUp(self): - self.in_channels = 3 - self.out_channels = 5 - self.kernel_size = [3, 3] - self.padding = [1, 1] - self.stride = [2, 2] - self.dilation = [1, 1] - self.deformable_groups = 1 - self.groups = 1 - self.no_bias = False - - -class TestDeformConv2DWithDeformable_Groups(TestDeformConv2D): - def setUp(self): - self.in_channels = 5 - self.out_channels = 5 - self.kernel_size = [3, 3] - self.padding = [1, 1] - self.stride = [1, 1] - self.dilation = [1, 1] - self.deformable_groups = 5 - self.groups = 1 - self.no_bias = False - - -class TestDeformConv2DWithGroups(TestDeformConv2D): - def setUp(self): - self.in_channels = 5 - self.out_channels = 5 - self.kernel_size = [3, 3] - self.padding = [1, 1] - self.stride = [1, 1] - self.dilation = [1, 1] - self.deformable_groups = 1 - self.groups = 5 - self.no_bias = False - - # testcases for deform_conv2d class TestDeformConv2DFunctionalWithPadding(TestDeformConv2DFunctional): def setUp(self): @@ -722,27 +423,5 @@ def setUp(self): self.no_bias = False -class TestDeformConv2DError(unittest.TestCase): - @test_with_pir_api - def test_input_error(self): - def test_input_rank_error(): - paddle.enable_static() - x = paddle.static.data(name='error_x_1', shape=[0], dtype='float32') - offset = paddle.static.data( - name='error_offset_1', shape=[0], dtype='float32' - ) - mask = paddle.static.data( - name='error_mask_1', shape=[0, 0, 0], dtype='float32' - ) - out = paddle.vision.ops.DeformConv2D( - in_channels=0, - out_channels=0, - kernel_size=0, - deformable_groups=0, - )(x, offset, mask) - - self.assertRaises(AssertionError, test_input_rank_error) - - if __name__ == "__main__": unittest.main() diff --git a/test/deprecated/legacy_test/test_deformable_conv_op_deprecated.py b/test/deprecated/legacy_test/test_deformable_conv_op_deprecated.py new file mode 100644 index 00000000000000..04bbc51d48fdaf --- /dev/null +++ b/test/deprecated/legacy_test/test_deformable_conv_op_deprecated.py @@ -0,0 +1,178 @@ +# 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. + +import unittest +from itertools import product + +import numpy as np + +import paddle + +paddle.enable_static() + + +def dmc_bilinear(data_im, height, width, h, w): + h_low = int(np.floor(h)) + w_low = int(np.floor(w)) + h_high = h_low + 1 + w_high = w_low + 1 + + lh = h - h_low + lw = w - w_low + hh = 1 - lh + hw = 1 - lw + + v1 = 0 + if h_low >= 0 and w_low >= 0: + v1 = data_im[h_low, w_low] + v2 = 0 + if h_low >= 0 and w_high <= width - 1: + v2 = data_im[h_low, w_high] + v3 = 0 + if h_high <= height - 1 and w_low >= 0: + v3 = data_im[h_high, w_low] + v4 = 0 + if h_high <= height - 1 and w_high <= width - 1: + v4 = data_im[h_high, w_high] + + w1, w2, w3, w4 = hh * hw, hh * lw, lh * hw, lh * lw + val = w1 * v1 + w2 * v2 + w3 * v3 + w4 * v4 + + return val + + +def dconv_im2col_gemm(input, offset, mask, filter, group, conv_param): + in_n, in_c, in_h, in_w = input.shape + out_c, f_c, f_h, f_w = filter.shape + + assert offset.shape == (in_n, 2 * f_h * f_w, in_h, in_w) + assert mask.shape == (in_n, f_h * f_w, in_h, in_w) + assert f_c * group == in_c + assert np.mod(out_c, group) == 0 + + stride, pad, dilation = ( + conv_param['stride'], + conv_param['pad'], + conv_param['dilation'], + ) + out_h = 1 + (in_h + 2 * pad[0] - (dilation[0] * (f_h - 1) + 1)) // stride[0] + out_w = 1 + (in_w + 2 * pad[1] - (dilation[1] * (f_w - 1) + 1)) // stride[1] + assert out_h == in_h + assert out_w == in_w + + col_buffer = np.zeros((in_n, in_c * f_h * f_w, in_h * in_w)) + for n, c, h, w, kh, kw in product( + range(in_n), + range(in_c), + range(out_h), + range(out_w), + range(f_h), + range(f_w), + ): + offset_h_table = offset[n, ::2, h, w].reshape(f_h, f_w) + offset_w_table = offset[n, 1::2, h, w].reshape(f_h, f_w) + mask_table = mask[n, :, h, w].reshape(f_h, f_w) + offset_h = offset_h_table[kh, kw] + offset_w = offset_w_table[kh, kw] + val = 0 + im_h = h * stride[0] + kh * dilation[0] + offset_h - pad[0] + im_w = w * stride[0] + kw * dilation[0] + offset_w - pad[1] + if im_h > -1 and im_w > -1 and im_h < in_h and im_w < in_h: + val = dmc_bilinear(input[n, c], in_h, in_w, im_h, im_w) + val_out = val * mask_table[kh, kw] + col_buffer[n, c * f_h * f_w + kh * f_w + kw, h * in_w + w] = val_out + + out = np.zeros((in_n, group, int(out_c // group), out_h * out_w)) + weight = filter.reshape(group, int(out_c // group), f_c * f_h * f_w) + col_buffer = col_buffer.reshape( + (in_n, group, int(in_c // group * f_h * f_w), in_h * in_w) + ) + for n in range(in_n): + for g in range(group): + out[n, g] = np.matmul(weight[g], col_buffer[n, g]) + out = out.reshape(in_n, out_c, out_h, out_w) + return out + + +class TestModulatedDeformableConvInvalidInput(unittest.TestCase): + def test_error(self): + def test_invalid_input(): + paddle.enable_static() + input = [1, 3, 32, 32] + offset = paddle.static.data( + name='offset', shape=[None, 3, 32, 32], dtype='float32' + ) + mask = paddle.static.data( + name='mask', shape=[None, 3, 32, 32], dtype='float32' + ) + loss = paddle.static.nn.common.deformable_conv( + input, offset, mask, num_filters=4, filter_size=1 + ) + + self.assertRaises(TypeError, test_invalid_input) + + def test_invalid_offset(): + paddle.enable_static() + input = paddle.static.data( + name='input', shape=[None, 3, 32, 32], dtype='int32' + ) + offset = paddle.static.data( + name='offset', shape=[None, 3, 32, 32], dtype='float32' + ) + mask = paddle.static.data( + name='mask', shape=[None, 3, 32, 32], dtype='float32' + ) + loss = paddle.static.nn.common.deformable_conv( + input, offset, mask, num_filters=4, filter_size=1 + ) + + self.assertRaises(TypeError, test_invalid_offset) + + def test_invalid_filter(): + paddle.enable_static() + input = paddle.static.data( + name='input_filter', shape=[None, 3, 32, 32], dtype='float32' + ) + offset = paddle.static.data( + name='offset_filter', shape=[None, 3, 32, 32], dtype='float32' + ) + mask = paddle.static.data( + name='mask_filter', shape=[None, 3, 32, 32], dtype='float32' + ) + loss = paddle.static.nn.common.deformable_conv( + input, offset, mask, num_filters=4, filter_size=0 + ) + + self.assertRaises(ValueError, test_invalid_filter) + + def test_invalid_groups(): + paddle.enable_static() + input = paddle.static.data( + name='input_groups', shape=[1, 1, 1, 1], dtype='float32' + ) + offset = paddle.static.data( + name='offset_groups', shape=[1, 1], dtype='float32' + ) + mask = paddle.static.data( + name='mask_groups', shape=[1], dtype='float32' + ) + paddle.static.nn.deform_conv2d( + input, offset, mask, 1, 1, padding=1, groups=0 + ) + + self.assertRaises(ValueError, test_invalid_groups) + + +if __name__ == '__main__': + unittest.main() diff --git a/test/deprecated/legacy_test/test_desc_clone.py b/test/deprecated/legacy_test/test_desc_clone_deprecated.py similarity index 100% rename from test/deprecated/legacy_test/test_desc_clone.py rename to test/deprecated/legacy_test/test_desc_clone_deprecated.py diff --git a/test/deprecated/legacy_test/test_device_guard.py b/test/deprecated/legacy_test/test_device_guard_deprecated.py similarity index 75% rename from test/deprecated/legacy_test/test_device_guard.py rename to test/deprecated/legacy_test/test_device_guard_deprecated.py index 502cde95f4007d..ddc2e1a1e09581 100644 --- a/test/deprecated/legacy_test/test_device_guard.py +++ b/test/deprecated/legacy_test/test_device_guard_deprecated.py @@ -93,57 +93,6 @@ def test_device_guard_with_id(self): execute(main_program, startup_program) - def test_cpu_only_op(self): - main_program = paddle.static.Program() - startup_program = paddle.static.Program() - with paddle.static.program_guard(main_program, startup_program): - x = paddle.full( - shape=[2, 255, 13, 13], fill_value=0.3, dtype='float32' - ) - gt_box = paddle.full( - shape=[2, 6, 4], fill_value=0.5, dtype='float32' - ) - gt_label = paddle.full(shape=[2, 6], fill_value=1.0, dtype='int32') - gt_score = paddle.full( - shape=[2, 6], fill_value=0.5, dtype='float32' - ) - anchors = [ - 10, - 13, - 16, - 30, - 33, - 23, - 30, - 61, - 62, - 45, - 59, - 119, - 116, - 90, - 156, - 198, - 373, - 326, - ] - anchor_mask = [0, 1, 2] - with paddle.static.device_guard("gpu"): - # yolo_loss only has cpu kernel, so its cpu kernel will be executed - loss = paddle.vision.ops.yolo_loss( - x=x, - gt_box=gt_box, - gt_label=gt_label, - gt_score=gt_score, - anchors=anchors, - anchor_mask=anchor_mask, - class_num=80, - ignore_thresh=0.7, - downsample_ratio=32, - ) - - execute(main_program, startup_program) - @test_with_pir_api def test_without_kernel_op(self): main_program = paddle.static.Program() @@ -174,18 +123,6 @@ def test_without_kernel_op(self): execute(main_program, startup_program) - def test_error(self): - def device_attr(): - with paddle.static.device_guard("cpu1"): - out = paddle.full(shape=[1], fill_value=0.2, dtype='float32') - - def device_attr2(): - with paddle.static.device_guard("cpu:1"): - out = paddle.full(shape=[1], fill_value=0.2, dtype='float32') - - self.assertRaises(ValueError, device_attr) - self.assertRaises(ValueError, device_attr2) - # check if op_descs have op_device attr def test_op_descs_device_attr(self): main_program = paddle.static.Program() diff --git a/test/deprecated/legacy_test/test_dist_fleet_a_sync_optimizer_auto_async.py b/test/deprecated/legacy_test/test_dist_fleet_a_sync_optimizer_auto_async_deprecated.py similarity index 100% rename from test/deprecated/legacy_test/test_dist_fleet_a_sync_optimizer_auto_async.py rename to test/deprecated/legacy_test/test_dist_fleet_a_sync_optimizer_auto_async_deprecated.py diff --git a/test/deprecated/legacy_test/test_dist_fleet_a_sync_optimizer_auto.py b/test/deprecated/legacy_test/test_dist_fleet_a_sync_optimizer_auto_deprecated.py similarity index 100% rename from test/deprecated/legacy_test/test_dist_fleet_a_sync_optimizer_auto.py rename to test/deprecated/legacy_test/test_dist_fleet_a_sync_optimizer_auto_deprecated.py diff --git a/test/deprecated/legacy_test/test_dist_fleet_a_sync_optimizer_auto_geo.py b/test/deprecated/legacy_test/test_dist_fleet_a_sync_optimizer_auto_geo_deprecated.py similarity index 100% rename from test/deprecated/legacy_test/test_dist_fleet_a_sync_optimizer_auto_geo.py rename to test/deprecated/legacy_test/test_dist_fleet_a_sync_optimizer_auto_geo_deprecated.py diff --git a/test/deprecated/legacy_test/test_dist_fleet_decay.py b/test/deprecated/legacy_test/test_dist_fleet_decay_deprecated.py similarity index 100% rename from test/deprecated/legacy_test/test_dist_fleet_decay.py rename to test/deprecated/legacy_test/test_dist_fleet_decay_deprecated.py diff --git a/test/deprecated/legacy_test/test_dist_fleet_heter_program.py b/test/deprecated/legacy_test/test_dist_fleet_heter_program_deprecated.py similarity index 100% rename from test/deprecated/legacy_test/test_dist_fleet_heter_program.py rename to test/deprecated/legacy_test/test_dist_fleet_heter_program_deprecated.py diff --git a/test/deprecated/legacy_test/test_dist_fleet_ps10.py b/test/deprecated/legacy_test/test_dist_fleet_ps10_deprecated.py similarity index 100% rename from test/deprecated/legacy_test/test_dist_fleet_ps10.py rename to test/deprecated/legacy_test/test_dist_fleet_ps10_deprecated.py diff --git a/test/deprecated/legacy_test/test_dist_fleet_ps13.py b/test/deprecated/legacy_test/test_dist_fleet_ps13_deprecated.py similarity index 100% rename from test/deprecated/legacy_test/test_dist_fleet_ps13.py rename to test/deprecated/legacy_test/test_dist_fleet_ps13_deprecated.py diff --git a/test/deprecated/legacy_test/test_dist_fleet_ps2.py b/test/deprecated/legacy_test/test_dist_fleet_ps2_deprecated.py similarity index 100% rename from test/deprecated/legacy_test/test_dist_fleet_ps2.py rename to test/deprecated/legacy_test/test_dist_fleet_ps2_deprecated.py diff --git a/test/deprecated/legacy_test/test_dist_fleet_ps3.py b/test/deprecated/legacy_test/test_dist_fleet_ps3_deprecated.py similarity index 100% rename from test/deprecated/legacy_test/test_dist_fleet_ps3.py rename to test/deprecated/legacy_test/test_dist_fleet_ps3_deprecated.py diff --git a/test/deprecated/legacy_test/test_dist_fleet_ps4.py b/test/deprecated/legacy_test/test_dist_fleet_ps4_deprecated.py similarity index 100% rename from test/deprecated/legacy_test/test_dist_fleet_ps4.py rename to test/deprecated/legacy_test/test_dist_fleet_ps4_deprecated.py diff --git a/test/deprecated/legacy_test/test_dist_fleet_ps5.py b/test/deprecated/legacy_test/test_dist_fleet_ps5_deprecated.py similarity index 100% rename from test/deprecated/legacy_test/test_dist_fleet_ps5.py rename to test/deprecated/legacy_test/test_dist_fleet_ps5_deprecated.py diff --git a/test/deprecated/legacy_test/test_dist_fleet_ps6.py b/test/deprecated/legacy_test/test_dist_fleet_ps6_deprecated.py similarity index 100% rename from test/deprecated/legacy_test/test_dist_fleet_ps6.py rename to test/deprecated/legacy_test/test_dist_fleet_ps6_deprecated.py diff --git a/test/deprecated/legacy_test/test_dist_fleet_ps7.py b/test/deprecated/legacy_test/test_dist_fleet_ps7_deprecated.py similarity index 100% rename from test/deprecated/legacy_test/test_dist_fleet_ps7.py rename to test/deprecated/legacy_test/test_dist_fleet_ps7_deprecated.py diff --git a/test/deprecated/legacy_test/test_dist_fleet_ps8.py b/test/deprecated/legacy_test/test_dist_fleet_ps8_deprecated.py similarity index 100% rename from test/deprecated/legacy_test/test_dist_fleet_ps8.py rename to test/deprecated/legacy_test/test_dist_fleet_ps8_deprecated.py diff --git a/test/deprecated/legacy_test/test_dist_fleet_ps9.py b/test/deprecated/legacy_test/test_dist_fleet_ps9_deprecated.py similarity index 100% rename from test/deprecated/legacy_test/test_dist_fleet_ps9.py rename to test/deprecated/legacy_test/test_dist_fleet_ps9_deprecated.py diff --git a/test/deprecated/legacy_test/test_dist_fleet_ps.py b/test/deprecated/legacy_test/test_dist_fleet_ps_deprecated.py similarity index 100% rename from test/deprecated/legacy_test/test_dist_fleet_ps.py rename to test/deprecated/legacy_test/test_dist_fleet_ps_deprecated.py diff --git a/test/deprecated/legacy_test/test_dist_sparse_tensor_load_sgd.py b/test/deprecated/legacy_test/test_dist_sparse_tensor_load_sgd_deprecated.py similarity index 100% rename from test/deprecated/legacy_test/test_dist_sparse_tensor_load_sgd.py rename to test/deprecated/legacy_test/test_dist_sparse_tensor_load_sgd_deprecated.py diff --git a/test/deprecated/legacy_test/test_eager_deletion_delete_vars.py b/test/deprecated/legacy_test/test_eager_deletion_delete_vars_deprecated.py similarity index 100% rename from test/deprecated/legacy_test/test_eager_deletion_delete_vars.py rename to test/deprecated/legacy_test/test_eager_deletion_delete_vars_deprecated.py diff --git a/test/deprecated/legacy_test/test_ema.py b/test/deprecated/legacy_test/test_ema_deprecated.py similarity index 99% rename from test/deprecated/legacy_test/test_ema.py rename to test/deprecated/legacy_test/test_ema_deprecated.py index acfd4479fe0967..9dbe53ba171767 100644 --- a/test/deprecated/legacy_test/test_ema.py +++ b/test/deprecated/legacy_test/test_ema_deprecated.py @@ -19,6 +19,8 @@ import paddle from paddle import base +paddle.enable_static() + class TestExponentialMovingAverage(unittest.TestCase): def setUp(self): diff --git a/test/deprecated/legacy_test/test_ema_fleet.py b/test/deprecated/legacy_test/test_ema_fleet_deprecated.py similarity index 100% rename from test/deprecated/legacy_test/test_ema_fleet.py rename to test/deprecated/legacy_test/test_ema_fleet_deprecated.py diff --git a/test/deprecated/legacy_test/test_embedding_id_stop_gradient.py b/test/deprecated/legacy_test/test_embedding_id_stop_gradient_deprecated.py similarity index 99% rename from test/deprecated/legacy_test/test_embedding_id_stop_gradient.py rename to test/deprecated/legacy_test/test_embedding_id_stop_gradient_deprecated.py index e39d9c4674c670..71eb6c67def0fa 100644 --- a/test/deprecated/legacy_test/test_embedding_id_stop_gradient.py +++ b/test/deprecated/legacy_test/test_embedding_id_stop_gradient_deprecated.py @@ -19,6 +19,8 @@ import paddle from paddle import base +paddle.enable_static() + class TestEmbeddingIdStopGradientBase(unittest.TestCase): def setUp(self): diff --git a/test/deprecated/legacy_test/test_entry_attr2.py b/test/deprecated/legacy_test/test_entry_attr2_deprecated.py similarity index 100% rename from test/deprecated/legacy_test/test_entry_attr2.py rename to test/deprecated/legacy_test/test_entry_attr2_deprecated.py diff --git a/test/deprecated/legacy_test/test_entry_attr.py b/test/deprecated/legacy_test/test_entry_attr_deprecated.py similarity index 100% rename from test/deprecated/legacy_test/test_entry_attr.py rename to test/deprecated/legacy_test/test_entry_attr_deprecated.py diff --git a/test/deprecated/legacy_test/test_error_clip.py b/test/deprecated/legacy_test/test_error_clip_deprecated.py similarity index 100% rename from test/deprecated/legacy_test/test_error_clip.py rename to test/deprecated/legacy_test/test_error_clip_deprecated.py diff --git a/test/deprecated/legacy_test/test_executor_and_use_program_cache_deprecated.py b/test/deprecated/legacy_test/test_executor_and_use_program_cache_deprecated.py new file mode 100644 index 00000000000000..d4f389f03b1c67 --- /dev/null +++ b/test/deprecated/legacy_test/test_executor_and_use_program_cache_deprecated.py @@ -0,0 +1,88 @@ +# 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. + +import sys +import unittest + +import numpy as np + +sys.path.append("../../legacy_test") + +import paddle +from paddle import base + + +class TestExecutor(unittest.TestCase): + def test_mul(self): + 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) + b.desc.set_need_check_feed(False) + output = paddle.matmul(x=a, y=b) + + # Compute with numpy + a_np = np.random.random((100, 784)).astype('float32') + b_np = np.random.random((784, 100)).astype('float32') + out_np = np.dot(a_np, b_np) + + place = paddle.CPUPlace() + exe = base.Executor(place) + + def _train(use_program_cache, max_iters=1): + import time + + run_time = 0.0 + for i in range(max_iters): + begin = time.time() + outs = exe.run( + program=main_program, + feed={'a': a_np, 'b': b_np}, + fetch_list=[output], + use_program_cache=use_program_cache, + ) + end = time.time() + run_time += end - begin + out = outs[0] + self.assertEqual((100, 100), out.shape) + np.testing.assert_allclose(out, out_np, rtol=1e-05) + return run_time + + max_iters = 3 + run_time_with_cache = _train( + use_program_cache=True, max_iters=max_iters + ) + print("run time with program cache: %f" % run_time_with_cache) + + run_time_without_cache = _train( + use_program_cache=False, max_iters=max_iters + ) + print("run time without program cache: %f" % run_time_without_cache) + + run_time_with_cache = _train( + use_program_cache=True, max_iters=max_iters + ) + print("run time with program cache: %f" % run_time_with_cache) + + run_time_with_cache = _train( + use_program_cache=True, max_iters=max_iters + ) + print("run time with program cache: %f" % run_time_with_cache) + + +if __name__ == '__main__': + paddle.enable_static() + unittest.main() diff --git a/test/deprecated/legacy_test/test_executor_check_feed.py b/test/deprecated/legacy_test/test_executor_check_feed_deprecated.py similarity index 99% rename from test/deprecated/legacy_test/test_executor_check_feed.py rename to test/deprecated/legacy_test/test_executor_check_feed_deprecated.py index 78fb383885ec44..59be3ede8229f4 100644 --- a/test/deprecated/legacy_test/test_executor_check_feed.py +++ b/test/deprecated/legacy_test/test_executor_check_feed_deprecated.py @@ -17,6 +17,8 @@ import paddle from paddle import base +paddle.enable_static() + class TestExecutor(unittest.TestCase): def net(self): diff --git a/test/deprecated/legacy_test/test_executor_feed_non_tensor.py b/test/deprecated/legacy_test/test_executor_feed_non_tensor.py index b472ccdc9158c7..ff3ff65e9652c6 100644 --- a/test/deprecated/legacy_test/test_executor_feed_non_tensor.py +++ b/test/deprecated/legacy_test/test_executor_feed_non_tensor.py @@ -14,130 +14,10 @@ import unittest -import numpy - import paddle from paddle import base -class TestExecutor(unittest.TestCase): - def net(self): - lr = 0.0 - x = paddle.static.data(name="x", shape=[None, 1], dtype='float32') - y = paddle.static.data(name="y", shape=[None, 1], dtype='float32') - y_predict = paddle.static.nn.fc(x, size=1) - - cost = paddle.nn.functional.square_error_cost(input=y_predict, label=y) - avg_cost = paddle.mean(cost) - - opt = paddle.optimizer.Adam(learning_rate=lr) - opt.minimize(avg_cost) - - return paddle.to_tensor(lr), avg_cost - - def test_program_feed_float(self): - 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( - 'float32' - ) - y_true = numpy.array([[2.0], [4.0], [6.0], [8.0]]).astype( - 'float32' - ) - a = 0.01 - _lr, _ = exe.run( - feed={'x': train_data, 'y': y_true, 'lr': a}, - fetch_list=[lr, cost], - return_numpy=False, - ) - self.assertEqual(_lr._dtype(), lr.dtype) - self.assertEqual(_lr._dtype(), paddle.float32) - self.assertEqual(type(a), float) - - def test_program_feed_int(self): - 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( - 'float32' - ) - y_true = numpy.array([[2.0], [4.0], [6.0], [8.0]]).astype( - 'float32' - ) - a = 0 - _lr, _ = exe.run( - feed={'x': train_data, 'y': y_true, 'lr': a}, - fetch_list=[lr, cost], - return_numpy=False, - ) - self.assertEqual(_lr._dtype(), lr.dtype) - self.assertEqual(_lr._dtype(), paddle.float32) - self.assertEqual(type(a), int) - - def test_program_feed_list(self): - 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]] - y_true = [[2.0], [4.0], [6.0], [8.0]] - a = 0 - _lr, _ = exe.run( - feed={'x': train_data, 'y': y_true, 'lr': a}, - fetch_list=[lr, cost], - return_numpy=False, - ) - self.assertEqual(_lr._dtype(), lr.dtype) - self.assertEqual(_lr._dtype(), paddle.float32) - self.assertEqual(type(y_true), list) - - def test_compiled_program_feed_scalar(self): - 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 = base.CPUPlace() - exe = base.Executor(cpu) - exe.run(startup_program) - compiled_prog = base.CompiledProgram(main_program) - train_data = numpy.array([[1.0], [2.0], [3.0], [4.0]]).astype( - 'float32' - ) - y_true = numpy.array([[2.0], [4.0], [6.0], [8.0]]).astype( - 'float32' - ) - a = 0.01 - _lr, _ = exe.run( - compiled_prog, - feed={'x': train_data, 'y': y_true, 'lr': a}, - fetch_list=[lr, cost], - return_numpy=False, - ) - self.assertEqual(_lr._dtype(), lr.dtype) - self.assertEqual(_lr._dtype(), paddle.float32) - self.assertEqual(type(a), float) - - class TestAsLodTensor(unittest.TestCase): def test_as_lodtensor_int32(self): cpu = base.CPUPlace() diff --git a/test/deprecated/legacy_test/test_executor_feed_non_tensor_deprecated.py b/test/deprecated/legacy_test/test_executor_feed_non_tensor_deprecated.py new file mode 100644 index 00000000000000..cbcdffd11fa2ef --- /dev/null +++ b/test/deprecated/legacy_test/test_executor_feed_non_tensor_deprecated.py @@ -0,0 +1,144 @@ +# Copyright (c) 2020 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 + +import paddle +from paddle import base + +paddle.enable_static() + + +class TestExecutor(unittest.TestCase): + def net(self): + lr = 0.0 + x = paddle.static.data(name="x", shape=[None, 1], dtype='float32') + y = paddle.static.data(name="y", shape=[None, 1], dtype='float32') + y_predict = paddle.static.nn.fc(x, size=1) + + cost = paddle.nn.functional.square_error_cost(input=y_predict, label=y) + avg_cost = paddle.mean(cost) + + opt = paddle.optimizer.Adam(learning_rate=lr) + opt.minimize(avg_cost) + + return paddle.to_tensor(lr), avg_cost + + def test_program_feed_float(self): + 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( + 'float32' + ) + y_true = numpy.array([[2.0], [4.0], [6.0], [8.0]]).astype( + 'float32' + ) + a = 0.01 + _lr, _ = exe.run( + feed={'x': train_data, 'y': y_true, 'lr': a}, + fetch_list=[lr, cost], + return_numpy=False, + ) + self.assertEqual(_lr._dtype(), lr.dtype) + self.assertEqual(_lr._dtype(), paddle.float32) + self.assertEqual(type(a), float) + + def test_program_feed_int(self): + 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( + 'float32' + ) + y_true = numpy.array([[2.0], [4.0], [6.0], [8.0]]).astype( + 'float32' + ) + a = 0 + _lr, _ = exe.run( + feed={'x': train_data, 'y': y_true, 'lr': a}, + fetch_list=[lr, cost], + return_numpy=False, + ) + self.assertEqual(_lr._dtype(), lr.dtype) + self.assertEqual(_lr._dtype(), paddle.float32) + self.assertEqual(type(a), int) + + def test_program_feed_list(self): + 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]] + y_true = [[2.0], [4.0], [6.0], [8.0]] + a = 0 + _lr, _ = exe.run( + feed={'x': train_data, 'y': y_true, 'lr': a}, + fetch_list=[lr, cost], + return_numpy=False, + ) + self.assertEqual(_lr._dtype(), lr.dtype) + self.assertEqual(_lr._dtype(), paddle.float32) + self.assertEqual(type(y_true), list) + + def test_compiled_program_feed_scalar(self): + 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 = base.CPUPlace() + exe = base.Executor(cpu) + exe.run(startup_program) + compiled_prog = base.CompiledProgram(main_program) + train_data = numpy.array([[1.0], [2.0], [3.0], [4.0]]).astype( + 'float32' + ) + y_true = numpy.array([[2.0], [4.0], [6.0], [8.0]]).astype( + 'float32' + ) + a = 0.01 + _lr, _ = exe.run( + compiled_prog, + feed={'x': train_data, 'y': y_true, 'lr': a}, + fetch_list=[lr, cost], + return_numpy=False, + ) + self.assertEqual(_lr._dtype(), lr.dtype) + self.assertEqual(_lr._dtype(), paddle.float32) + self.assertEqual(type(a), float) + + +if __name__ == '__main__': + unittest.main() diff --git a/test/deprecated/legacy_test/test_fc_op.py b/test/deprecated/legacy_test/test_fc_op_deprecated.py similarity index 66% rename from test/deprecated/legacy_test/test_fc_op.py rename to test/deprecated/legacy_test/test_fc_op_deprecated.py index d3d2008c17e157..961fb6e006bad1 100644 --- a/test/deprecated/legacy_test/test_fc_op.py +++ b/test/deprecated/legacy_test/test_fc_op_deprecated.py @@ -15,7 +15,7 @@ import unittest import numpy as np -from op_test import OpTest, paddle_static_guard +from op_test import paddle_static_guard import paddle from paddle import base @@ -54,88 +54,6 @@ def __init__(self, mb, ic, oc, h, w, bias_dims=2): self.bias = np.random.random(oc).astype("float32") -class TestFCOp(OpTest): - def config(self): - self.with_bias = True - self.with_relu = True - self.matrix = MatrixGenerate(1, 10, 15, 3, 3, 2) - - def setUp(self): - self.op_type = "fc" - self.config() - - if self.with_bias: - self.inputs = { - 'Input': self.matrix.input, - 'W': self.matrix.weights, - 'Bias': self.matrix.bias, - } - else: - self.inputs = {'Input': self.matrix.input, 'W': self.matrix.weights} - - if self.with_relu: - activation_type = "relu" - else: - activation_type = "" - self.attrs = {'use_mkldnn': False, 'activation_type': activation_type} - - self.outputs = { - 'Out': fc_refer(self.matrix, self.with_bias, self.with_relu) - } - - def test_check_output(self): - self.check_output(check_dygraph=False) - - -class TestFCOpNoBias1(TestFCOp): - def config(self): - self.with_bias = False - self.with_relu = False - self.matrix = MatrixGenerate(2, 8, 10, 1, 1, 2) - - -class TestFCOpNoBias2(TestFCOp): - def config(self): - self.with_bias = False - self.with_relu = False - self.matrix = MatrixGenerate(4, 5, 6, 2, 2, 1) - - -class TestFCOpNoBias4(TestFCOp): - def config(self): - self.with_bias = False - self.with_relu = False - self.matrix = MatrixGenerate(1, 32, 64, 3, 3, 1) - - -class TestFCOpWithBias1(TestFCOp): - def config(self): - self.with_bias = True - self.with_relu = False - self.matrix = MatrixGenerate(3, 8, 10, 2, 1, 2) - - -class TestFCOpWithBias2(TestFCOp): - def config(self): - self.with_bias = True - self.with_relu = True - self.matrix = MatrixGenerate(4, 5, 6, 2, 2, 1) - - -class TestFCOpWithBias3(TestFCOp): - def config(self): - self.with_bias = True - self.with_relu = True - self.matrix = MatrixGenerate(1, 64, 32, 3, 3, 1) - - -class TestFCOpWithPadding(TestFCOp): - def config(self): - self.with_bias = True - self.with_relu = True - self.matrix = MatrixGenerate(1, 4, 3, 128, 128, 2) - - class TestFcOp_NumFlattenDims_NegOne(unittest.TestCase): def test_api(self): def run_program(num_flatten_dims): diff --git a/test/deprecated/legacy_test/test_feed_data_check_shape_type.py b/test/deprecated/legacy_test/test_feed_data_check_shape_type_deprecated.py similarity index 99% rename from test/deprecated/legacy_test/test_feed_data_check_shape_type.py rename to test/deprecated/legacy_test/test_feed_data_check_shape_type_deprecated.py index f89247860ba74c..40858d81f65dd6 100644 --- a/test/deprecated/legacy_test/test_feed_data_check_shape_type.py +++ b/test/deprecated/legacy_test/test_feed_data_check_shape_type_deprecated.py @@ -22,6 +22,8 @@ from paddle import base from paddle.base import core +paddle.enable_static() + os.environ['CPU_NUM'] = str(4) np.random.seed(123) diff --git a/test/deprecated/legacy_test/test_fleet_base.py b/test/deprecated/legacy_test/test_fleet_base.py index 2ffd8a747c72d4..a8b3203b131c22 100644 --- a/test/deprecated/legacy_test/test_fleet_base.py +++ b/test/deprecated/legacy_test/test_fleet_base.py @@ -18,7 +18,6 @@ import numpy as np import paddle -from paddle import base from paddle.distributed import fleet from paddle.distributed.fleet.base import role_maker @@ -184,45 +183,5 @@ def test_dygraph_method(self): final_strategy = fleet._final_strategy() -class TestFleetBaseSingleError(unittest.TestCase): - def setUp(self): - os.environ.pop("PADDLE_TRAINER_ENDPOINTS") - - def gen_data(self): - return { - "x": np.random.random(size=(128, 32)).astype('float32'), - "y": np.random.randint(2, size=(128, 1)).astype('int64'), - } - - def test_single_run_collective_minimize(self): - def test_single_error(): - input_x = paddle.static.data( - name="x", shape=[-1, 32], dtype='float32' - ) - input_y = paddle.static.data(name="y", shape=[-1, 1], dtype='int64') - - fc_1 = paddle.static.nn.fc(x=input_x, size=64, activation='tanh') - prediction = paddle.static.nn.fc( - x=fc_1, size=2, activation='softmax' - ) - cost = paddle.nn.functional.cross_entropy( - input=prediction, - label=input_y, - reduction='none', - use_softmax=False, - ) - avg_cost = paddle.mean(x=cost) - fleet.init(is_collective=True) - - # in non_distributed mode(use `python` to launch), raise error if has multi cards - if ( - base.core.is_compiled_with_cuda() - and base.core.get_cuda_device_count() > 1 - ): - self.assertRaises(ValueError, test_single_error) - else: - test_single_error() - - if __name__ == "__main__": unittest.main() diff --git a/test/deprecated/legacy_test/test_fleet_base_2.py b/test/deprecated/legacy_test/test_fleet_base_2_deprecated.py similarity index 100% rename from test/deprecated/legacy_test/test_fleet_base_2.py rename to test/deprecated/legacy_test/test_fleet_base_2_deprecated.py diff --git a/test/deprecated/legacy_test/test_fleet_base_3.py b/test/deprecated/legacy_test/test_fleet_base_3_deprecated.py similarity index 100% rename from test/deprecated/legacy_test/test_fleet_base_3.py rename to test/deprecated/legacy_test/test_fleet_base_3_deprecated.py diff --git a/test/deprecated/legacy_test/test_fleet_base_deprecated.py b/test/deprecated/legacy_test/test_fleet_base_deprecated.py new file mode 100644 index 00000000000000..8fa62c24e3a4cf --- /dev/null +++ b/test/deprecated/legacy_test/test_fleet_base_deprecated.py @@ -0,0 +1,66 @@ +# Copyright (c) 2020 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 unittest + +import numpy as np + +import paddle +from paddle import base +from paddle.distributed import fleet + + +class TestFleetBaseSingleError(unittest.TestCase): + def setUp(self): + os.environ.pop("PADDLE_TRAINER_ENDPOINTS") + + def gen_data(self): + return { + "x": np.random.random(size=(128, 32)).astype('float32'), + "y": np.random.randint(2, size=(128, 1)).astype('int64'), + } + + def test_single_run_collective_minimize(self): + def test_single_error(): + input_x = paddle.static.data( + name="x", shape=[-1, 32], dtype='float32' + ) + input_y = paddle.static.data(name="y", shape=[-1, 1], dtype='int64') + + fc_1 = paddle.static.nn.fc(x=input_x, size=64, activation='tanh') + prediction = paddle.static.nn.fc( + x=fc_1, size=2, activation='softmax' + ) + cost = paddle.nn.functional.cross_entropy( + input=prediction, + label=input_y, + reduction='none', + use_softmax=False, + ) + avg_cost = paddle.mean(x=cost) + fleet.init(is_collective=True) + + # in non_distributed mode(use `python` to launch), raise error if has multi cards + if ( + base.core.is_compiled_with_cuda() + and base.core.get_cuda_device_count() > 1 + ): + self.assertRaises(ValueError, test_single_error) + else: + test_single_error() + + +if __name__ == "__main__": + unittest.main() diff --git a/test/deprecated/legacy_test/test_fleet.py b/test/deprecated/legacy_test/test_fleet_deprecated.py similarity index 99% rename from test/deprecated/legacy_test/test_fleet.py rename to test/deprecated/legacy_test/test_fleet_deprecated.py index 0e9eb0579cc98d..0c39931932649c 100644 --- a/test/deprecated/legacy_test/test_fleet.py +++ b/test/deprecated/legacy_test/test_fleet_deprecated.py @@ -16,6 +16,10 @@ import os import unittest +import paddle + +paddle.enable_static() + class TestFleet1(unittest.TestCase): """ diff --git a/test/deprecated/legacy_test/test_fleet_nocvm_1.py b/test/deprecated/legacy_test/test_fleet_nocvm_1_deprecated.py similarity index 99% rename from test/deprecated/legacy_test/test_fleet_nocvm_1.py rename to test/deprecated/legacy_test/test_fleet_nocvm_1_deprecated.py index d9962c1a27b387..331047d3cb3253 100644 --- a/test/deprecated/legacy_test/test_fleet_nocvm_1.py +++ b/test/deprecated/legacy_test/test_fleet_nocvm_1_deprecated.py @@ -18,6 +18,8 @@ import paddle +paddle.enable_static() + class TestFleet1(unittest.TestCase): """ diff --git a/test/deprecated/legacy_test/test_fleet_unitaccessor.py b/test/deprecated/legacy_test/test_fleet_unitaccessor_deprecated.py similarity index 99% rename from test/deprecated/legacy_test/test_fleet_unitaccessor.py rename to test/deprecated/legacy_test/test_fleet_unitaccessor_deprecated.py index f6e33ed1ee6b38..faf5487e4bf9ab 100644 --- a/test/deprecated/legacy_test/test_fleet_unitaccessor.py +++ b/test/deprecated/legacy_test/test_fleet_unitaccessor_deprecated.py @@ -18,6 +18,8 @@ import paddle +paddle.enable_static() + class TestFleet1(unittest.TestCase): """ diff --git a/test/deprecated/legacy_test/test_functional_conv2d.py b/test/deprecated/legacy_test/test_functional_conv2d_deprecated.py similarity index 64% rename from test/deprecated/legacy_test/test_functional_conv2d.py rename to test/deprecated/legacy_test/test_functional_conv2d_deprecated.py index 0cc69fb0281a46..4e4ea507b092e5 100644 --- a/test/deprecated/legacy_test/test_functional_conv2d.py +++ b/test/deprecated/legacy_test/test_functional_conv2d_deprecated.py @@ -196,76 +196,6 @@ def test_identity_gpu(self): self._test_identity() -class TestFunctionalConv2DError(TestCase): - batch_size = 4 - spatial_shape = (16, 16) - dtype = "float32" - - def setUp(self): - self.in_channels = 3 - self.out_channels = 5 - self.filter_shape = 3 - self.padding = "not_valid" - self.stride = 1 - self.dilation = 1 - self.groups = 1 - self.no_bias = False - self.act = "sigmoid" - self.data_format = "NHWC" - - def test_exception(self): - self.prepare() - with self.assertRaises(ValueError): - self.static_graph_case() - - def prepare(self): - if isinstance(self.filter_shape, int): - filter_shape = (self.filter_shape,) * 2 - else: - filter_shape = tuple(self.filter_shape) - self.weight_shape = ( - self.out_channels, - self.in_channels // self.groups, - ) + filter_shape - self.bias_shape = (self.out_channels,) - - def static_graph_case(self): - 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( - "input", - (-1, -1, -1, self.in_channels), - dtype=self.dtype, - ) - else: - x = paddle.static.data( - "input", - (-1, self.in_channels, -1, -1), - dtype=self.dtype, - ) - weight = paddle.static.data( - "weight", self.weight_shape, dtype=self.dtype - ) - if not self.no_bias: - bias = paddle.static.data( - "bias", self.bias_shape, dtype=self.dtype - ) - y = F.conv2d( - x, - weight, - None if self.no_bias else bias, - padding=self.padding, - stride=self.stride, - dilation=self.dilation, - groups=self.groups, - data_format=self.data_format, - ) - - class TestFunctionalConv2DCase2(TestFunctionalConv2D): def setUp(self): self.in_channels = 3 @@ -371,126 +301,6 @@ def setUp(self): self.data_format = "NCHW" -class TestFunctionalConv2DErrorCase2(TestFunctionalConv2DError): - def setUp(self): - self.in_channels = 3 - self.out_channels = 5 - self.filter_shape = 3 - self.padding = [[0, 0], [1, 2], [3, 4], [5, 6]] - self.stride = 1 - self.dilation = 1 - self.groups = 1 - self.no_bias = False - self.act = "sigmoid" - self.use_cudnn = False - self.data_format = "NCHW" - - -class TestFunctionalConv2DErrorCase3(TestFunctionalConv2DError): - def setUp(self): - self.in_channels = 3 - self.out_channels = 4 - self.filter_shape = 3 - self.padding = "same" - self.stride = 1 - self.dilation = 1 - self.groups = 2 - self.no_bias = False - self.act = "sigmoid" - self.use_cudnn = False - self.data_format = "not_valid" - - -class TestFunctionalConv2DErrorCase4(TestFunctionalConv2DError): - def setUp(self): - self.in_channels = 4 - self.out_channels = 3 - self.filter_shape = 3 - self.padding = "same" - self.stride = 1 - self.dilation = 1 - self.groups = 2 - self.no_bias = False - self.act = "sigmoid" - self.use_cudnn = False - self.data_format = "NCHW" - - -class TestFunctionalConv2DErrorCase7(TestFunctionalConv2DError): - def setUp(self): - self.in_channels = 3 - self.out_channels = 5 - self.filter_shape = 3 - self.padding = "same" - self.stride = 1 - self.dilation = 1 - self.groups = 1 - self.no_bias = False - self.act = "sigmoid" - self.use_cudnn = True - self.data_format = "not_valid" - - -class TestFunctionalConv2DErrorCase8(TestFunctionalConv2DError): - def setUp(self): - self.in_channels = 3 - self.out_channels = 5 - self.filter_shape = 3 - self.padding = [1, 2, 1, 2, 1] - self.stride = 1 - self.dilation = 1 - self.groups = 1 - self.no_bias = False - self.act = "sigmoid" - self.use_cudnn = True - self.data_format = "NCHW" - - -class TestFunctionalConv2DErrorCase9(TestFunctionalConv2DError): - def setUp(self): - self.in_channels = -5 - self.out_channels = 5 - self.filter_shape = 3 - self.padding = [[0, 0], [0, 0], [3, 2], [1, 2]] - self.stride = 1 - self.dilation = 1 - self.groups = 1 - self.no_bias = False - self.act = "sigmoid" - self.use_cudnn = False - self.data_format = "NCHW" - - -class TestFunctionalConv2DErrorCase10(TestFunctionalConv2DError): - def setUp(self): - self.in_channels = 3 - self.out_channels = 4 - self.filter_shape = 3 - self.padding = "same" - self.stride = 1 - self.dilation = 1 - self.groups = 2 - self.no_bias = False - self.act = "sigmoid" - self.use_cudnn = False - self.data_format = "NHWC" - - -class TestFunctionalConv2DErrorCase11(TestFunctionalConv2DError): - def setUp(self): - self.in_channels = 3 - self.out_channels = 5 - self.filter_shape = 3 - self.padding = 0 - self.stride = 1 - self.dilation = 1 - self.groups = 1 - self.no_bias = False - self.act = "sigmoid" - self.use_cudnn = False - self.data_format = "NHCW" - - class TestFunctionalConv2DErrorCase12(TestCase): def setUp(self): self.input = np.array([]) @@ -532,30 +342,6 @@ def static_graph_case(self): (out,) = exe.run(main, feed={"input": self.input}, fetch_list=[y]) return out - def dygraph_case(self): - with dg.guard(): - x = paddle.to_tensor(self.input, dtype=paddle.float32) - w = paddle.to_tensor(self.filter, dtype=paddle.float32) - b = ( - None - if self.bias is None - else paddle.to_tensor(self.bias, dtype=paddle.float32) - ) - y = F.conv2d( - x, - w, - b, - padding=self.padding, - stride=self.stride, - dilation=self.dilation, - groups=self.groups, - data_format=self.data_format, - ) - - def test_dygraph_exception(self): - with self.assertRaises(ValueError): - self.dygraph_case() - def test_static_exception(self): with self.assertRaises(ValueError): self.static_graph_case() diff --git a/test/deprecated/legacy_test/test_functional_conv2d_transpose.py b/test/deprecated/legacy_test/test_functional_conv2d_transpose_deprecated.py similarity index 99% rename from test/deprecated/legacy_test/test_functional_conv2d_transpose.py rename to test/deprecated/legacy_test/test_functional_conv2d_transpose_deprecated.py index c8d5f88af6de36..8fb2393bbd7a9e 100644 --- a/test/deprecated/legacy_test/test_functional_conv2d_transpose.py +++ b/test/deprecated/legacy_test/test_functional_conv2d_transpose_deprecated.py @@ -22,6 +22,8 @@ import paddle.nn.functional as F from paddle import base +paddle.enable_static() + class TestFunctionalConv2D(TestCase): batch_size = 4 diff --git a/test/deprecated/legacy_test/test_functional_conv3d.py b/test/deprecated/legacy_test/test_functional_conv3d_deprecated.py similarity index 66% rename from test/deprecated/legacy_test/test_functional_conv3d.py rename to test/deprecated/legacy_test/test_functional_conv3d_deprecated.py index 6634d0194670b0..75be067d4a261a 100644 --- a/test/deprecated/legacy_test/test_functional_conv3d.py +++ b/test/deprecated/legacy_test/test_functional_conv3d_deprecated.py @@ -196,79 +196,6 @@ def test_identity_gpu(self): self._test_identity() -class TestFunctionalConv3DError(TestCase): - batch_size = 4 - spatial_shape = (8, 8, 8) - dtype = "float32" - - def setUp(self): - self.in_channels = 3 - self.out_channels = 5 - self.filter_shape = 3 - self.padding = "not_valid" - self.stride = 1 - self.dilation = 1 - self.groups = 1 - self.no_bias = False - self.act = "sigmoid" - self.data_format = "NDHWC" - - def test_exception(self): - self.prepare() - with self.assertRaises(ValueError): - self.static_graph_case() - - def prepare(self): - if isinstance(self.filter_shape, int): - filter_shape = (self.filter_shape,) * 3 - else: - filter_shape = tuple(self.filter_shape) - self.weight_shape = ( - self.out_channels, - self.in_channels // self.groups, - ) + filter_shape - self.bias_shape = (self.out_channels,) - - def static_graph_case(self): - 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( - "input", - (-1, -1, -1, -1, self.in_channels), - dtype=self.dtype, - ) - else: - x = paddle.static.data( - "input", - (-1, self.in_channels, -1, -1, -1), - dtype=self.dtype, - ) - weight = paddle.static.data( - "weight", self.weight_shape, dtype=self.dtype - ) - if not self.no_bias: - bias = paddle.static.data( - "bias", self.bias_shape, dtype=self.dtype - ) - y = F.conv3d( - x, - weight, - None if self.no_bias else bias, - padding=self.padding, - stride=self.stride, - dilation=self.dilation, - groups=self.groups, - data_format=self.data_format, - ) - - if self.act == 'sigmoid': - y = F.sigmoid(y) - - class TestFunctionalConv3DCase2(TestFunctionalConv3D): def setUp(self): self.in_channels = 3 @@ -368,104 +295,6 @@ def setUp(self): self.data_format = "NCDHW" -class TestFunctionalConv3DErrorCase2(TestFunctionalConv3DError): - def setUp(self): - self.in_channels = 3 - self.out_channels = 5 - self.filter_shape = 3 - self.padding = [[0, 0], [1, 1], [1, 2], [3, 4], [5, 6]] - self.stride = 1 - self.dilation = 1 - self.groups = 1 - self.no_bias = False - self.act = "sigmoid" - self.data_format = "NCDHW" - - -class TestFunctionalConv3DErrorCase3(TestFunctionalConv3DError): - def setUp(self): - self.in_channels = 3 - self.out_channels = 4 - self.filter_shape = 3 - self.padding = "same" - self.stride = 1 - self.dilation = 1 - self.groups = 2 - self.no_bias = False - self.act = "sigmoid" - self.data_format = "not_valid" - - -class TestFunctionalConv3DErrorCase4(TestFunctionalConv3DError): - def setUp(self): - self.in_channels = 4 - self.out_channels = 3 - self.filter_shape = 3 - self.padding = "same" - self.stride = 1 - self.dilation = 1 - self.groups = 2 - self.no_bias = False - self.act = "sigmoid" - self.data_format = "NCDHW" - - -class TestFunctionalConv3DErrorCase7(TestFunctionalConv3DError): - def setUp(self): - self.in_channels = 3 - self.out_channels = 5 - self.filter_shape = 3 - self.padding = "same" - self.stride = 1 - self.dilation = 1 - self.groups = 1 - self.no_bias = False - self.act = "sigmoid" - self.data_format = "not_valid" - - -class TestFunctionalConv3DErrorCase8(TestFunctionalConv3DError): - def setUp(self): - self.in_channels = 3 - self.out_channels = 5 - self.filter_shape = 3 - self.padding = [1, 2, 1, 2, 1] - self.stride = 1 - self.dilation = 1 - self.groups = 1 - self.no_bias = False - self.act = "sigmoid" - self.data_format = "NCDHW" - - -class TestFunctionalConv3DErrorCase9(TestFunctionalConv3DError): - def setUp(self): - self.in_channels = -5 - self.out_channels = 5 - self.filter_shape = 3 - self.padding = [[0, 0], [0, 0], [3, 2], [1, 2], [1, 1]] - self.stride = 1 - self.dilation = 1 - self.groups = 1 - self.no_bias = False - self.act = "sigmoid" - self.data_format = "NCDHW" - - -class TestFunctionalConv3DErrorCase10(TestFunctionalConv3DError): - def setUp(self): - self.in_channels = 3 - self.out_channels = 4 - self.filter_shape = 3 - self.padding = "same" - self.stride = 1 - self.dilation = 1 - self.groups = 2 - self.no_bias = False - self.act = "sigmoid" - self.data_format = "NDHWC" - - class TestFunctionalConv3DErrorCase11(TestCase): def setUp(self): self.input = np.array([]) @@ -507,30 +336,6 @@ def static_graph_case(self): (out,) = exe.run(main, feed={"input": self.input}, fetch_list=[y]) return out - def dygraph_case(self): - with dg.guard(): - x = paddle.to_tensor(self.input, dtype=paddle.float32) - w = paddle.to_tensor(self.filter, dtype=paddle.float32) - b = ( - None - if self.bias is None - else paddle.to_tensor(self.bias, dtype=paddle.float32) - ) - y = F.conv3d( - x, - w, - b, - padding=self.padding, - stride=self.stride, - dilation=self.dilation, - groups=self.groups, - data_format=self.data_format, - ) - - def test_dygraph_exception(self): - with self.assertRaises(ValueError): - self.dygraph_case() - def test_static_exception(self): with self.assertRaises(ValueError): self.static_graph_case() diff --git a/test/deprecated/legacy_test/test_functional_conv3d_transpose.py b/test/deprecated/legacy_test/test_functional_conv3d_transpose.py index a4ea020dd29965..5f5ee23b04cfeb 100644 --- a/test/deprecated/legacy_test/test_functional_conv3d_transpose.py +++ b/test/deprecated/legacy_test/test_functional_conv3d_transpose.py @@ -23,179 +23,6 @@ from paddle import base -class TestFunctionalConv3DTranspose(TestCase): - batch_size = 4 - spatial_shape = (8, 8, 8) - dtype = "float32" - output_size = None - - def setUp(self): - self.in_channels = 3 - self.out_channels = 5 - self.filter_shape = 3 - self.padding = 0 - self.stride = 1 - self.dilation = 1 - self.groups = 1 - self.no_bias = False - self.act = "sigmoid" - self.data_format = "NDHWC" - - def prepare(self): - if isinstance(self.filter_shape, int): - filter_shape = (self.filter_shape,) * 3 - else: - filter_shape = tuple(self.filter_shape) - - self.weight = np.random.uniform( - -1, - 1, - (self.in_channels, self.out_channels // self.groups) + filter_shape, - ).astype(self.dtype) - if not self.no_bias: - self.bias = np.random.uniform(-1, 1, (self.out_channels,)).astype( - self.dtype - ) - - self.channel_last = self.data_format == "NDHWC" - if self.channel_last: - self.input_shape = ( - (self.batch_size,) + self.spatial_shape + (self.in_channels,) - ) - else: - self.input_shape = ( - self.batch_size, - self.in_channels, - ) + self.spatial_shape - - self.input = np.random.uniform(-1, 1, self.input_shape).astype( - self.dtype - ) - - def static_graph_case_1(self): - 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", - (-1, -1, -1, -1, self.in_channels), - dtype=self.dtype, - ) - else: - x = paddle.static.data( - "input", - (-1, self.in_channels, -1, -1, -1), - dtype=self.dtype, - ) - y = paddle.static.nn.conv3d_transpose( - x, - self.out_channels, - output_size=self.output_size, - filter_size=self.filter_shape, - stride=self.stride, - padding=self.padding, - dilation=self.dilation, - groups=self.groups, - param_attr=paddle.nn.initializer.Assign(self.weight), - bias_attr=False - if self.no_bias - else paddle.nn.initializer.Assign(self.bias), - act=self.act, - data_format=self.data_format, - ) - 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 = 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", - (-1, -1, -1, -1, self.in_channels), - dtype=self.dtype, - ) - else: - x = paddle.static.data( - "input", - (-1, self.in_channels, -1, -1, -1), - dtype=self.dtype, - ) - weight = paddle.static.data( - "weight", self.weight.shape, dtype=self.dtype - ) - if not self.no_bias: - bias = paddle.static.data( - "bias", self.bias.shape, dtype=self.dtype - ) - y = F.conv3d_transpose( - x, - weight, - None if self.no_bias else bias, - output_size=self.output_size, - padding=self.padding, - stride=self.stride, - dilation=self.dilation, - groups=self.groups, - data_format=self.data_format, - ) - if self.act == 'sigmoid': - y = F.sigmoid(y) - exe = base.Executor(self.place) - exe.run(start) - feed_dict = {"input": self.input, "weight": self.weight} - if not self.no_bias: - feed_dict["bias"] = self.bias - (out,) = exe.run(main, feed=feed_dict, fetch_list=[y]) - return out - - def dygraph_case(self): - with dg.guard(self.place): - x = paddle.to_tensor(self.input) - weight = paddle.to_tensor(self.weight) - bias = None if self.no_bias else paddle.to_tensor(self.bias) - y = F.conv3d_transpose( - x, - weight, - bias, - output_size=self.output_size, - padding=self.padding, - stride=self.stride, - dilation=self.dilation, - groups=self.groups, - data_format=self.data_format, - ) - if self.act == 'sigmoid': - y = F.sigmoid(y) - out = y.numpy() - return out - - def _test_identity(self): - self.prepare() - out1 = self.static_graph_case_1() - out2 = self.static_graph_case_2() - out3 = self.dygraph_case() - np.testing.assert_array_almost_equal(out1, out2) - np.testing.assert_array_almost_equal(out2, out3) - - def test_identity_cpu(self): - self.place = base.CPUPlace() - self._test_identity() - - @unittest.skipIf( - not base.core.is_compiled_with_cuda(), "core is not compiled with CUDA" - ) - def test_identity_gpu(self): - self.place = base.CUDAPlace(0) - self._test_identity() - - class TestFunctionalConv3DTransposeError(TestCase): batch_size = 4 spatial_shape = (8, 8, 8) @@ -270,147 +97,6 @@ def static_graph_case(self): y = F.sigmoid(y) -class TestFunctionalConv3DTransposeCase2(TestFunctionalConv3DTranspose): - def setUp(self): - self.in_channels = 3 - self.out_channels = 5 - self.filter_shape = 3 - self.padding = 0 - self.stride = 1 - self.dilation = 1 - self.groups = 1 - self.no_bias = False - self.act = "sigmoid" - self.data_format = "NCDHW" - - -class TestFunctionalConv3DTransposeCase3(TestFunctionalConv3DTranspose): - def setUp(self): - self.in_channels = 4 - self.out_channels = 6 - self.filter_shape = 3 - self.padding = 0 - self.stride = 1 - self.dilation = 1 - self.groups = 2 - self.no_bias = False - self.act = "sigmoid" - self.data_format = "NDHWC" - - -class TestFunctionalConv3DTransposeCase4(TestFunctionalConv3DTranspose): - def setUp(self): - self.in_channels = 4 - self.out_channels = 6 - self.filter_shape = 3 - self.padding = "same" - self.stride = 1 - self.dilation = 1 - self.groups = 2 - self.no_bias = True - self.act = "sigmoid" - self.data_format = "NDHWC" - - -class TestFunctionalConv3DTransposeCase5(TestFunctionalConv3DTranspose): - def setUp(self): - self.in_channels = 4 - self.out_channels = 6 - self.filter_shape = 3 - self.padding = "valid" - self.stride = (1, 2, 1) - self.dilation = (2, 1, 1) - self.groups = 2 - self.no_bias = False - self.act = "sigmoid" - self.data_format = "NDHWC" - - -class TestFunctionalConv3DTransposeCase6(TestFunctionalConv3DTranspose): - def setUp(self): - self.in_channels = 4 - self.out_channels = 4 - self.filter_shape = 3 - self.padding = "valid" - self.stride = (1, 2, 1) - self.dilation = 1 - self.groups = 4 - self.no_bias = False - self.act = "sigmoid" - self.data_format = "NDHWC" - - -class TestFunctionalConv3DTransposeCase7(TestFunctionalConv3DTranspose): - def setUp(self): - self.in_channels = 4 - self.out_channels = 4 - self.filter_shape = 3 - self.padding = "valid" - self.output_size = (10, 17, 10) - self.stride = (1, 2, 1) - self.dilation = 1 - self.groups = 1 - self.no_bias = False - self.act = "sigmoid" - self.data_format = "NCDHW" - - -class TestFunctionalConv3DTransposeCase8(TestFunctionalConv3DTranspose): - def setUp(self): - self.in_channels = 4 - self.out_channels = 6 - self.filter_shape = 3 - self.padding = [[0, 0], [1, 2], [1, 2], [2, 1], [0, 0]] - self.stride = 1 - self.dilation = 1 - self.groups = 2 - self.no_bias = False - self.act = "sigmoid" - self.data_format = "NDHWC" - - -class TestFunctionalConv3DTransposeCase9(TestFunctionalConv3DTranspose): - def setUp(self): - self.in_channels = 4 - self.out_channels = 6 - self.filter_shape = 3 - self.padding = [[0, 0], [0, 0], [1, 1], [1, 1], [2, 2]] - self.stride = 1 - self.dilation = 1 - self.groups = 2 - self.no_bias = False - self.act = "sigmoid" - self.data_format = "NCDHW" - - -class TestFunctionalConv3DTransposeCase10(TestFunctionalConv3DTranspose): - def setUp(self): - self.in_channels = 4 - self.out_channels = 6 - self.filter_shape = 3 - self.padding = [1, 1, 2, 2, 1, 1] - self.stride = 1 - self.dilation = 1 - self.groups = 2 - self.no_bias = False - self.act = "sigmoid" - self.data_format = "NCDHW" - - -class TestFunctionalConv3DTransposeCase11(TestFunctionalConv3DTranspose): - def setUp(self): - self.in_channels = 4 - self.out_channels = 6 - self.filter_shape = 3 - self.padding = [1, 2, 1] - self.stride = 1 - self.dilation = 1 - self.groups = 2 - self.no_bias = False - self.act = "sigmoid" - self.data_format = "NCDHW" - - class TestFunctionalConv3DTransposeErrorCase2( TestFunctionalConv3DTransposeError ): @@ -537,34 +223,6 @@ def setUp(self): self.groups = 1 self.data_format = "NCDHW" - def static_graph_case(self): - 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 - ) - y = paddle.static.nn.conv3d_transpose( - x, - self.num_filters, - self.filter_size, - stride=self.stride, - padding=self.padding, - dilation=self.dilation, - groups=self.groups, - param_attr=paddle.nn.initializer.Assign(self.filter), - bias_attr=False - if self.bias is None - else paddle.nn.initializer.Assign(self.bias), - act=None, - data_format=self.data_format, - ) - exe = base.Executor() - exe.run(start) - (out,) = exe.run(main, feed={"input": self.input}, fetch_list=[y]) - return out - def dygraph_case(self): with dg.guard(): x = paddle.to_tensor(self.input, dtype=paddle.float32) @@ -589,10 +247,6 @@ def test_dygraph_exception(self): with self.assertRaises(ValueError): self.dygraph_case() - def test_static_exception(self): - with self.assertRaises(ValueError): - self.static_graph_case() - class TestFunctionalConv3DTransposeErrorCase11( TestFunctionalConv3DTransposeErrorCase10 diff --git a/test/deprecated/legacy_test/test_functional_conv3d_transpose_deprecated.py b/test/deprecated/legacy_test/test_functional_conv3d_transpose_deprecated.py new file mode 100644 index 00000000000000..5be6713d7f2e88 --- /dev/null +++ b/test/deprecated/legacy_test/test_functional_conv3d_transpose_deprecated.py @@ -0,0 +1,405 @@ +# Copyright (c) 2020 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 unittest import TestCase + +import numpy as np + +import paddle +import paddle.base.dygraph as dg +import paddle.nn.functional as F +from paddle import base + +paddle.enable_static() + + +class TestFunctionalConv3DTranspose(TestCase): + batch_size = 4 + spatial_shape = (8, 8, 8) + dtype = "float32" + output_size = None + + def setUp(self): + self.in_channels = 3 + self.out_channels = 5 + self.filter_shape = 3 + self.padding = 0 + self.stride = 1 + self.dilation = 1 + self.groups = 1 + self.no_bias = False + self.act = "sigmoid" + self.data_format = "NDHWC" + + def prepare(self): + if isinstance(self.filter_shape, int): + filter_shape = (self.filter_shape,) * 3 + else: + filter_shape = tuple(self.filter_shape) + + self.weight = np.random.uniform( + -1, + 1, + (self.in_channels, self.out_channels // self.groups) + filter_shape, + ).astype(self.dtype) + if not self.no_bias: + self.bias = np.random.uniform(-1, 1, (self.out_channels,)).astype( + self.dtype + ) + + self.channel_last = self.data_format == "NDHWC" + if self.channel_last: + self.input_shape = ( + (self.batch_size,) + self.spatial_shape + (self.in_channels,) + ) + else: + self.input_shape = ( + self.batch_size, + self.in_channels, + ) + self.spatial_shape + + self.input = np.random.uniform(-1, 1, self.input_shape).astype( + self.dtype + ) + + def static_graph_case_1(self): + 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", + (-1, -1, -1, -1, self.in_channels), + dtype=self.dtype, + ) + else: + x = paddle.static.data( + "input", + (-1, self.in_channels, -1, -1, -1), + dtype=self.dtype, + ) + y = paddle.static.nn.conv3d_transpose( + x, + self.out_channels, + output_size=self.output_size, + filter_size=self.filter_shape, + stride=self.stride, + padding=self.padding, + dilation=self.dilation, + groups=self.groups, + param_attr=paddle.nn.initializer.Assign(self.weight), + bias_attr=False + if self.no_bias + else paddle.nn.initializer.Assign(self.bias), + act=self.act, + data_format=self.data_format, + ) + 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 = 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", + (-1, -1, -1, -1, self.in_channels), + dtype=self.dtype, + ) + else: + x = paddle.static.data( + "input", + (-1, self.in_channels, -1, -1, -1), + dtype=self.dtype, + ) + weight = paddle.static.data( + "weight", self.weight.shape, dtype=self.dtype + ) + if not self.no_bias: + bias = paddle.static.data( + "bias", self.bias.shape, dtype=self.dtype + ) + y = F.conv3d_transpose( + x, + weight, + None if self.no_bias else bias, + output_size=self.output_size, + padding=self.padding, + stride=self.stride, + dilation=self.dilation, + groups=self.groups, + data_format=self.data_format, + ) + if self.act == 'sigmoid': + y = F.sigmoid(y) + exe = base.Executor(self.place) + exe.run(start) + feed_dict = {"input": self.input, "weight": self.weight} + if not self.no_bias: + feed_dict["bias"] = self.bias + (out,) = exe.run(main, feed=feed_dict, fetch_list=[y]) + return out + + def dygraph_case(self): + with dg.guard(self.place): + x = paddle.to_tensor(self.input) + weight = paddle.to_tensor(self.weight) + bias = None if self.no_bias else paddle.to_tensor(self.bias) + y = F.conv3d_transpose( + x, + weight, + bias, + output_size=self.output_size, + padding=self.padding, + stride=self.stride, + dilation=self.dilation, + groups=self.groups, + data_format=self.data_format, + ) + if self.act == 'sigmoid': + y = F.sigmoid(y) + out = y.numpy() + return out + + def _test_identity(self): + self.prepare() + out1 = self.static_graph_case_1() + out2 = self.static_graph_case_2() + out3 = self.dygraph_case() + np.testing.assert_array_almost_equal(out1, out2) + np.testing.assert_array_almost_equal(out2, out3) + + def test_identity_cpu(self): + self.place = base.CPUPlace() + self._test_identity() + + @unittest.skipIf( + not base.core.is_compiled_with_cuda(), "core is not compiled with CUDA" + ) + def test_identity_gpu(self): + self.place = base.CUDAPlace(0) + self._test_identity() + + +class TestFunctionalConv3DTransposeCase2(TestFunctionalConv3DTranspose): + def setUp(self): + self.in_channels = 3 + self.out_channels = 5 + self.filter_shape = 3 + self.padding = 0 + self.stride = 1 + self.dilation = 1 + self.groups = 1 + self.no_bias = False + self.act = "sigmoid" + self.data_format = "NCDHW" + + +class TestFunctionalConv3DTransposeCase3(TestFunctionalConv3DTranspose): + def setUp(self): + self.in_channels = 4 + self.out_channels = 6 + self.filter_shape = 3 + self.padding = 0 + self.stride = 1 + self.dilation = 1 + self.groups = 2 + self.no_bias = False + self.act = "sigmoid" + self.data_format = "NDHWC" + + +class TestFunctionalConv3DTransposeCase4(TestFunctionalConv3DTranspose): + def setUp(self): + self.in_channels = 4 + self.out_channels = 6 + self.filter_shape = 3 + self.padding = "same" + self.stride = 1 + self.dilation = 1 + self.groups = 2 + self.no_bias = True + self.act = "sigmoid" + self.data_format = "NDHWC" + + +class TestFunctionalConv3DTransposeCase5(TestFunctionalConv3DTranspose): + def setUp(self): + self.in_channels = 4 + self.out_channels = 6 + self.filter_shape = 3 + self.padding = "valid" + self.stride = (1, 2, 1) + self.dilation = (2, 1, 1) + self.groups = 2 + self.no_bias = False + self.act = "sigmoid" + self.data_format = "NDHWC" + + +class TestFunctionalConv3DTransposeCase6(TestFunctionalConv3DTranspose): + def setUp(self): + self.in_channels = 4 + self.out_channels = 4 + self.filter_shape = 3 + self.padding = "valid" + self.stride = (1, 2, 1) + self.dilation = 1 + self.groups = 4 + self.no_bias = False + self.act = "sigmoid" + self.data_format = "NDHWC" + + +class TestFunctionalConv3DTransposeCase7(TestFunctionalConv3DTranspose): + def setUp(self): + self.in_channels = 4 + self.out_channels = 4 + self.filter_shape = 3 + self.padding = "valid" + self.output_size = (10, 17, 10) + self.stride = (1, 2, 1) + self.dilation = 1 + self.groups = 1 + self.no_bias = False + self.act = "sigmoid" + self.data_format = "NCDHW" + + +class TestFunctionalConv3DTransposeCase8(TestFunctionalConv3DTranspose): + def setUp(self): + self.in_channels = 4 + self.out_channels = 6 + self.filter_shape = 3 + self.padding = [[0, 0], [1, 2], [1, 2], [2, 1], [0, 0]] + self.stride = 1 + self.dilation = 1 + self.groups = 2 + self.no_bias = False + self.act = "sigmoid" + self.data_format = "NDHWC" + + +class TestFunctionalConv3DTransposeCase9(TestFunctionalConv3DTranspose): + def setUp(self): + self.in_channels = 4 + self.out_channels = 6 + self.filter_shape = 3 + self.padding = [[0, 0], [0, 0], [1, 1], [1, 1], [2, 2]] + self.stride = 1 + self.dilation = 1 + self.groups = 2 + self.no_bias = False + self.act = "sigmoid" + self.data_format = "NCDHW" + + +class TestFunctionalConv3DTransposeCase10(TestFunctionalConv3DTranspose): + def setUp(self): + self.in_channels = 4 + self.out_channels = 6 + self.filter_shape = 3 + self.padding = [1, 1, 2, 2, 1, 1] + self.stride = 1 + self.dilation = 1 + self.groups = 2 + self.no_bias = False + self.act = "sigmoid" + self.data_format = "NCDHW" + + +class TestFunctionalConv3DTransposeCase11(TestFunctionalConv3DTranspose): + def setUp(self): + self.in_channels = 4 + self.out_channels = 6 + self.filter_shape = 3 + self.padding = [1, 2, 1] + self.stride = 1 + self.dilation = 1 + self.groups = 2 + self.no_bias = False + self.act = "sigmoid" + self.data_format = "NCDHW" + + +class TestFunctionalConv3DTransposeErrorCase10(TestCase): + def setUp(self): + self.input = np.array([]) + self.filter = np.array([]) + self.num_filters = 0 + self.filter_size = 0 + self.bias = None + self.padding = 0 + self.stride = 1 + self.dilation = 1 + self.groups = 1 + self.data_format = "NCDHW" + + def static_graph_case(self): + 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 + ) + y = paddle.static.nn.conv3d_transpose( + x, + self.num_filters, + self.filter_size, + stride=self.stride, + padding=self.padding, + dilation=self.dilation, + groups=self.groups, + param_attr=paddle.nn.initializer.Assign(self.filter), + bias_attr=False + if self.bias is None + else paddle.nn.initializer.Assign(self.bias), + act=None, + data_format=self.data_format, + ) + exe = base.Executor() + exe.run(start) + (out,) = exe.run(main, feed={"input": self.input}, fetch_list=[y]) + return out + + def test_static_exception(self): + with self.assertRaises(ValueError): + self.static_graph_case() + + +class TestFunctionalConv3DTransposeErrorCase11( + TestFunctionalConv3DTransposeErrorCase10 +): + def setUp(self): + self.input = np.random.randn(1, 3, 3, 3, 3) + self.filter = np.random.randn(3, 3, 1, 1, 1) + self.num_filters = 3 + self.filter_size = 1 + self.bias = None + self.padding = 0 + self.stride = 1 + self.dilation = 1 + self.groups = 0 + self.data_format = "NCDHW" + + +if __name__ == "__main__": + unittest.main() diff --git a/test/deprecated/legacy_test/test_fuse_bn_act_pass.py b/test/deprecated/legacy_test/test_fuse_bn_act_pass_deprecated.py similarity index 99% rename from test/deprecated/legacy_test/test_fuse_bn_act_pass.py rename to test/deprecated/legacy_test/test_fuse_bn_act_pass_deprecated.py index 6faaff59b51efe..958cfe70dcc0dc 100644 --- a/test/deprecated/legacy_test/test_fuse_bn_act_pass.py +++ b/test/deprecated/legacy_test/test_fuse_bn_act_pass_deprecated.py @@ -17,6 +17,8 @@ import paddle from paddle import base +paddle.enable_static() + class TestFuseBatchNormActPass(unittest.TestCase): def build_program(self, main_program, startup_program, use_cuda, seed=1): diff --git a/test/deprecated/legacy_test/test_get_inputs_outputs_in_block.py b/test/deprecated/legacy_test/test_get_inputs_outputs_in_block_deprecated.py similarity index 100% rename from test/deprecated/legacy_test/test_get_inputs_outputs_in_block.py rename to test/deprecated/legacy_test/test_get_inputs_outputs_in_block_deprecated.py diff --git a/test/deprecated/legacy_test/test_gradient_clip.py b/test/deprecated/legacy_test/test_gradient_clip_deprecated.py similarity index 100% rename from test/deprecated/legacy_test/test_gradient_clip.py rename to test/deprecated/legacy_test/test_gradient_clip_deprecated.py diff --git a/test/deprecated/legacy_test/test_hsigmoid_op_deprecated.py b/test/deprecated/legacy_test/test_hsigmoid_op_deprecated.py new file mode 100644 index 00000000000000..574bc03172a4f7 --- /dev/null +++ b/test/deprecated/legacy_test/test_hsigmoid_op_deprecated.py @@ -0,0 +1,113 @@ +# Copyright (c) 2018 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 math +import unittest + +import numpy as np + +import paddle +from paddle import base + +paddle.enable_static() +np.random.seed(100) + + +class TestHSigmoidOpWithSparseGrad(unittest.TestCase): + def hs_net_conf(self, is_sparse): + input_word = paddle.static.data(name="x", shape=[-1, 1], dtype='int64') + path_table = paddle.static.data( + name='path_table', shape=[-1, 3], dtype='int64' + ) + path_code = paddle.static.data( + name='path_code', shape=[-1, 3], dtype='int64' + ) + label = paddle.static.data(name='label', shape=[-1, 1], dtype='int64') + + data_list = [input_word, path_table, path_code, label] + + emb = paddle.static.nn.embedding( + input=input_word, + is_sparse=is_sparse, + size=[3, 3], + param_attr=base.ParamAttr( + initializer=paddle.nn.initializer.Normal(std=1 / math.sqrt(3)) + ), + ) + + loss = paddle.nn.HSigmoidLoss( + feature_size=emb.shape[1], + num_classes=3, + bias_attr=True, + is_custom=True, + is_sparse=is_sparse, + ) + + cost = loss( + input=emb, + label=label, + path_table=path_table, + path_code=path_code, + ) + + avg_cost = paddle.mean(cost) + + return avg_cost, data_list + + def training_test(self, is_sparse): + with paddle.static.program_guard( + paddle.static.Program(), paddle.static.Program() + ): + paddle.seed(1) + start_up = paddle.static.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') + label = np.array([1, 4]).astype('int64') + + loss, data_list = self.hs_net_conf(is_sparse) + optimizer = paddle.optimizer.SGD(learning_rate=1e-3) + optimizer.minimize(loss) + + main_program = paddle.static.default_main_program() + place = base.CPUPlace() + feeder = base.DataFeeder(feed_list=data_list, place=place) + exe = paddle.static.Executor(place) + + exe.run(start_up) + result = [] + for i in range(10): + data = [ + ( + [[x[i % 2]]], + [list(path_table[i % 2])], + [list(path_code[i % 2])], + [label[i % 2]], + ) + ] + + loss_val = exe.run( + main_program, feed=feeder.feed(data), fetch_list=[loss] + ) + result.append(loss_val) + return result + + def test_hs_grad_with_sparse(self): + dense_result = self.training_test(is_sparse=False) + sparse_result = self.training_test(is_sparse=True) + assert dense_result == sparse_result + + +if __name__ == '__main__': + unittest.main() diff --git a/test/deprecated/legacy_test/test_image_classification_layer.py b/test/deprecated/legacy_test/test_image_classification_layer.py index cacffb437bad06..c786db344d47dd 100644 --- a/test/deprecated/legacy_test/test_image_classification_layer.py +++ b/test/deprecated/legacy_test/test_image_classification_layer.py @@ -38,21 +38,6 @@ def conv_block(input, num_filter, groups, dropouts): class TestLayer(unittest.TestCase): - def test_batch_norm_layer(self): - main_program = Program() - startup_program = Program() - with base.program_guard(main_program, startup_program): - images = paddle.static.data( - name='pixel', shape=[-1, 3, 48, 48], dtype='float32' - ) - hidden1 = paddle.static.nn.batch_norm(input=images) - hidden2 = paddle.static.nn.fc( - x=hidden1, size=128, activation='relu' - ) - paddle.static.nn.batch_norm(input=hidden2) - - print(str(main_program)) - def test_dropout_layer(self): main_program = Program() startup_program = Program() diff --git a/test/deprecated/legacy_test/test_image_classification_layer_deprecated.py b/test/deprecated/legacy_test/test_image_classification_layer_deprecated.py new file mode 100644 index 00000000000000..a977388a352834 --- /dev/null +++ b/test/deprecated/legacy_test/test_image_classification_layer_deprecated.py @@ -0,0 +1,60 @@ +# Copyright (c) 2018 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 + +sys.path.append("../../legacy_test") +import nets + +import paddle +from paddle import base +from paddle.base.framework import Program + +paddle.enable_static() + + +def conv_block(input, num_filter, groups, dropouts): + return nets.img_conv_group( + input=input, + pool_size=2, + pool_stride=2, + conv_num_filter=[num_filter] * groups, + conv_filter_size=3, + conv_act='relu', + conv_with_batchnorm=True, + conv_batchnorm_drop_rate=dropouts, + pool_type='max', + ) + + +class TestLayer(unittest.TestCase): + def test_batch_norm_layer(self): + main_program = Program() + startup_program = Program() + with base.program_guard(main_program, startup_program): + images = paddle.static.data( + name='pixel', shape=[-1, 3, 48, 48], dtype='float32' + ) + hidden1 = paddle.static.nn.batch_norm(input=images) + hidden2 = paddle.static.nn.fc( + x=hidden1, size=128, activation='relu' + ) + paddle.static.nn.batch_norm(input=hidden2) + + print(str(main_program)) + + +if __name__ == '__main__': + unittest.main() diff --git a/test/deprecated/legacy_test/test_imperative_double_grad.py b/test/deprecated/legacy_test/test_imperative_double_grad.py index eca85b0cbf58e4..0baf4369b0716e 100644 --- a/test/deprecated/legacy_test/test_imperative_double_grad.py +++ b/test/deprecated/legacy_test/test_imperative_double_grad.py @@ -567,21 +567,6 @@ def model_f(input): np.testing.assert_array_equal(grad_1, grad_2) -class TestRaiseNoDoubleGradOp(TestCase): - def test_no_grad_op(self): - 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 = base.dygraph.grad( - outputs=[y], inputs=[x], create_graph=True, retain_graph=True - )[0] - - loss = paddle.mean(dx) - loss.backward() - - class TestDoubleGradResNet(TestCase): def setUp(self): paddle.seed(123) diff --git a/test/deprecated/legacy_test/test_imperative_double_grad_deprecated.py b/test/deprecated/legacy_test/test_imperative_double_grad_deprecated.py new file mode 100644 index 00000000000000..9fda4f4d3dc1fb --- /dev/null +++ b/test/deprecated/legacy_test/test_imperative_double_grad_deprecated.py @@ -0,0 +1,38 @@ +# 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 unittest +from unittest import TestCase + +import paddle +from paddle import base + + +class TestRaiseNoDoubleGradOp(TestCase): + def test_no_grad_op(self): + 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 = base.dygraph.grad( + outputs=[y], inputs=[x], create_graph=True, retain_graph=True + )[0] + + loss = paddle.mean(dx) + loss.backward() + + +if __name__ == '__main__': + unittest.main() diff --git a/test/deprecated/legacy_test/test_imperative_load_static_param.py b/test/deprecated/legacy_test/test_imperative_load_static_param_deprecated.py similarity index 100% rename from test/deprecated/legacy_test/test_imperative_load_static_param.py rename to test/deprecated/legacy_test/test_imperative_load_static_param_deprecated.py diff --git a/test/deprecated/legacy_test/test_imperative_lod_tensor_to_selected_rows.py b/test/deprecated/legacy_test/test_imperative_lod_tensor_to_selected_rows_deprecated.py similarity index 100% rename from test/deprecated/legacy_test/test_imperative_lod_tensor_to_selected_rows.py rename to test/deprecated/legacy_test/test_imperative_lod_tensor_to_selected_rows_deprecated.py diff --git a/test/deprecated/legacy_test/test_infer_no_need_buffer_slots.py b/test/deprecated/legacy_test/test_infer_no_need_buffer_slots_deprecated.py similarity index 100% rename from test/deprecated/legacy_test/test_infer_no_need_buffer_slots.py rename to test/deprecated/legacy_test/test_infer_no_need_buffer_slots_deprecated.py diff --git a/test/deprecated/legacy_test/test_inference_api.py b/test/deprecated/legacy_test/test_inference_api_deprecated.py similarity index 81% rename from test/deprecated/legacy_test/test_inference_api.py rename to test/deprecated/legacy_test/test_inference_api_deprecated.py index b6f5456ee47962..aba8f4cf82b863 100644 --- a/test/deprecated/legacy_test/test_inference_api.py +++ b/test/deprecated/legacy_test/test_inference_api_deprecated.py @@ -20,7 +20,6 @@ import numpy as np from paddle import base -from paddle.base.core import PaddleDType, PaddleTensor from paddle.framework import core from paddle.inference import ( Config, @@ -30,49 +29,6 @@ ) -class TestInferenceApi(unittest.TestCase): - def test_inference_api(self): - tensor32 = np.random.randint(10, 20, size=[20, 2]).astype('int32') - paddletensor32 = PaddleTensor(tensor32) - dtype32 = paddletensor32.dtype - self.assertEqual(dtype32, PaddleDType.INT32) - self.assertEqual( - paddletensor32.data.tolist('int32'), tensor32.ravel().tolist() - ) - paddletensor32.data.reset(tensor32) - self.assertEqual( - paddletensor32.as_ndarray().ravel().tolist(), - tensor32.ravel().tolist(), - ) - - tensor64 = np.random.randint(10, 20, size=[20, 2]).astype('int64') - paddletensor64 = PaddleTensor(tensor64) - dtype64 = paddletensor64.dtype - self.assertEqual(dtype64, PaddleDType.INT64) - self.assertEqual( - paddletensor64.data.tolist('int64'), tensor64.ravel().tolist() - ) - paddletensor64.data.reset(tensor64) - self.assertEqual( - paddletensor64.as_ndarray().ravel().tolist(), - tensor64.ravel().tolist(), - ) - - tensor_float = np.random.randn(20, 2).astype('float32') - paddletensor_float = PaddleTensor(tensor_float) - dtype_float = paddletensor_float.dtype - self.assertEqual(dtype_float, PaddleDType.FLOAT32) - self.assertEqual( - paddletensor_float.data.tolist('float32'), - tensor_float.ravel().tolist(), - ) - paddletensor_float.data.reset(tensor_float) - self.assertEqual( - paddletensor_float.as_ndarray().ravel().tolist(), - tensor_float.ravel().tolist(), - ) - - def get_sample_model(): place = base.CPUPlace() exe = base.Executor(place) diff --git a/test/deprecated/legacy_test/test_inference_model_io.py b/test/deprecated/legacy_test/test_inference_model_io_deprecated.py similarity index 97% rename from test/deprecated/legacy_test/test_inference_model_io.py rename to test/deprecated/legacy_test/test_inference_model_io_deprecated.py index 3b9d486e791e4b..c01bd2d92d9d02 100644 --- a/test/deprecated/legacy_test/test_inference_model_io.py +++ b/test/deprecated/legacy_test/test_inference_model_io_deprecated.py @@ -555,21 +555,5 @@ def test_normalize_program(self): ) -class TestLoadInferenceModelError(unittest.TestCase): - @test_with_pir_api - def test_load_model_not_exist(self): - place = core.CPUPlace() - exe = executor.Executor(place) - self.assertRaises( - ValueError, load_inference_model, './test_not_exist_dir/model', exe - ) - self.assertRaises( - ValueError, - load_inference_model_distributed, - './test_not_exist_dir', - exe, - ) - - if __name__ == '__main__': unittest.main() diff --git a/test/deprecated/legacy_test/test_initializer.py b/test/deprecated/legacy_test/test_initializer.py index 5910a9c4297e09..c55940afe59034 100644 --- a/test/deprecated/legacy_test/test_initializer.py +++ b/test/deprecated/legacy_test/test_initializer.py @@ -20,7 +20,6 @@ from utils import dygraph_guard, static_guard import paddle -from paddle import base from paddle.base import framework from paddle.base.core import VarDesc from paddle.pir_utils import test_with_pir_api @@ -1448,65 +1447,6 @@ def test_numpy_array_initializer_bf16(self): self.assertTrue(check_cast_op_pir(cast_op)) -class TestSetGlobalInitializer(unittest.TestCase): - def test_set_global_weight_initializer(self): - """Test Set Global Param initializer with UniformInitializer""" - main_prog = framework.Program() - startup_prog = framework.Program() - base.set_global_initializer( - paddle.nn.initializer.Uniform(low=-0.5, high=0.5) - ) - with base.program_guard(main_prog, startup_prog): - x = paddle.static.data(name="x", shape=[1, 3, 32, 32]) - # default initializer of param in layers.conv2d is NormalInitializer - conv = paddle.static.nn.conv2d(x, 5, 3) - - block = startup_prog.global_block() - self.assertEqual(len(block.ops), 2) - - # init weight is the first op, and bias is the second - bias_init_op = block.ops[1] - self.assertEqual(bias_init_op.type, 'fill_constant') - self.assertAlmostEqual(bias_init_op.attr('value'), 0.0, delta=DELTA) - - param_init_op = block.ops[0] - self.assertEqual(param_init_op.type, 'uniform_random') - 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) - base.set_global_initializer(None) - - def test_set_global_bias_initializer(self): - """Test Set Global Bias initializer with NormalInitializer""" - main_prog = framework.Program() - startup_prog = framework.Program() - 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 base.program_guard(main_prog, startup_prog): - x = paddle.static.data(name="x", shape=[1, 3, 32, 32]) - # default initializer of bias in layers.conv2d is ConstantInitializer - conv = paddle.static.nn.conv2d(x, 5, 3) - - block = startup_prog.global_block() - self.assertEqual(len(block.ops), 2) - - # init weight is the first op, and bias is the second - bias_init_op = block.ops[1] - self.assertEqual(bias_init_op.type, 'gaussian_random') - self.assertAlmostEqual(bias_init_op.attr('mean'), 0.0, delta=DELTA) - self.assertAlmostEqual(bias_init_op.attr('std'), 2.0, delta=DELTA) - self.assertEqual(bias_init_op.attr('seed'), 0) - - param_init_op = block.ops[0] - self.assertEqual(param_init_op.type, 'uniform_random') - 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) - base.set_global_initializer(None) - - class TestUniformInitializerDygraph(unittest.TestCase): def test_uniform_initializer(self, dtype="float32"): """ @@ -2192,22 +2132,6 @@ def test_error(self): paddle.nn.Conv2D(5, 9, (3, 3), weight_attr=self.weight_attr) -class TestKaimingUniform(unittest.TestCase): - def func_kaiminguniform_initializer_fan_in_zero(self): - paddle.enable_static() - x = paddle.static.data(name='x', shape=[1, 0, 0], dtype='float32') - - kaiming = paddle.nn.initializer.KaimingUniform(0) - param_attr = paddle.ParamAttr(initializer=kaiming) - - paddle.static.nn.prelu(x, 'all', param_attr=param_attr) - - def test_type_error(self): - self.assertRaises( - ZeroDivisionError, self.func_kaiminguniform_initializer_fan_in_zero - ) - - class TestTruncatedNormalInitializerDygraph(unittest.TestCase): def _trunc_normal_numpy(self, tensor, mean=0.0, std=1.0, a=-2.0, b=2.0): # Method based on https://people.sc.fsu.edu/~jburkardt/presentations/truncated_normal.pdf diff --git a/test/deprecated/legacy_test/test_initializer_deprecated.py b/test/deprecated/legacy_test/test_initializer_deprecated.py new file mode 100644 index 00000000000000..75473cee68b7ae --- /dev/null +++ b/test/deprecated/legacy_test/test_initializer_deprecated.py @@ -0,0 +1,101 @@ +# Copyright (c) 2018 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 base +from paddle.base import framework + +DELTA = 0.00001 + + +class TestSetGlobalInitializer(unittest.TestCase): + def test_set_global_weight_initializer(self): + """Test Set Global Param initializer with UniformInitializer""" + main_prog = framework.Program() + startup_prog = framework.Program() + base.set_global_initializer( + paddle.nn.initializer.Uniform(low=-0.5, high=0.5) + ) + with base.program_guard(main_prog, startup_prog): + x = paddle.static.data(name="x", shape=[1, 3, 32, 32]) + # default initializer of param in layers.conv2d is NormalInitializer + conv = paddle.static.nn.conv2d(x, 5, 3) + + block = startup_prog.global_block() + self.assertEqual(len(block.ops), 2) + + # init weight is the first op, and bias is the second + bias_init_op = block.ops[1] + self.assertEqual(bias_init_op.type, 'fill_constant') + self.assertAlmostEqual(bias_init_op.attr('value'), 0.0, delta=DELTA) + + param_init_op = block.ops[0] + self.assertEqual(param_init_op.type, 'uniform_random') + 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) + base.set_global_initializer(None) + + def test_set_global_bias_initializer(self): + """Test Set Global Bias initializer with NormalInitializer""" + main_prog = framework.Program() + startup_prog = framework.Program() + 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 base.program_guard(main_prog, startup_prog): + x = paddle.static.data(name="x", shape=[1, 3, 32, 32]) + # default initializer of bias in layers.conv2d is ConstantInitializer + conv = paddle.static.nn.conv2d(x, 5, 3) + + block = startup_prog.global_block() + self.assertEqual(len(block.ops), 2) + + # init weight is the first op, and bias is the second + bias_init_op = block.ops[1] + self.assertEqual(bias_init_op.type, 'gaussian_random') + self.assertAlmostEqual(bias_init_op.attr('mean'), 0.0, delta=DELTA) + self.assertAlmostEqual(bias_init_op.attr('std'), 2.0, delta=DELTA) + self.assertEqual(bias_init_op.attr('seed'), 0) + + param_init_op = block.ops[0] + self.assertEqual(param_init_op.type, 'uniform_random') + 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) + base.set_global_initializer(None) + + +class TestKaimingUniform(unittest.TestCase): + def func_kaiminguniform_initializer_fan_in_zero(self): + paddle.enable_static() + x = paddle.static.data(name='x', shape=[1, 0, 0], dtype='float32') + + kaiming = paddle.nn.initializer.KaimingUniform(0) + param_attr = paddle.ParamAttr(initializer=kaiming) + + paddle.static.nn.prelu(x, 'all', param_attr=param_attr) + + def test_type_error(self): + self.assertRaises( + ZeroDivisionError, self.func_kaiminguniform_initializer_fan_in_zero + ) + + +if __name__ == '__main__': + paddle.enable_static() + unittest.main() diff --git a/test/deprecated/legacy_test/test_instance_norm_op.py b/test/deprecated/legacy_test/test_instance_norm_op.py index 2e9f9855d10332..b266e67dfd3346 100644 --- a/test/deprecated/legacy_test/test_instance_norm_op.py +++ b/test/deprecated/legacy_test/test_instance_norm_op.py @@ -21,7 +21,7 @@ import paddle from paddle import base, nn -from paddle.base import Program, core, program_guard +from paddle.base import core def _reference_instance_norm_naive(x, scale, bias, epsilon, mean, var): @@ -722,184 +722,6 @@ def init_test_case(self): ) -class TestInstanceNormOpTraining(unittest.TestCase): - def setUp(self): - self.epsilon = 1e-5 - self.init_test_case() - - def init_test_case(self): - self.shape = [2, 3, 4, 5] - self.no_grad_set = set() - self.fetch_list = [ - 'y', - 'saved_mean', - 'saved_variance', - 'x@GRAD', - 'scale@GRAD', - 'bias@GRAD', - ] - - def __assert_close(self, tensor, np_array, msg, atol=1e-4): - np.testing.assert_allclose( - np.array(tensor), np_array, rtol=1e-05, atol=atol, err_msg=msg - ) - - def set_global_mean_var(self, mean_shape, x): - mean, variance = _cal_mean_variance(x, self.epsilon, mean_shape) - return mean, variance - - def test_forward_backward(self): - def test_with_place(place, shape): - paddle.enable_static() - epsilon = self.epsilon - n, c, h, w = shape[0], shape[1], shape[2], shape[3] - scale_shape = [c] - mean_shape = [n * c] - - np.random.seed() - x = np.random.random_sample(shape).astype(np.float32) - scale = np.random.random_sample(scale_shape).astype(np.float32) - bias = np.random.random_sample(scale_shape).astype(np.float32) - mean, variance = self.set_global_mean_var(mean_shape, x) - d_y = np.random.random_sample(shape).astype(np.float32) - - y, saved_mean, variance_tmp = _reference_instance_norm_naive( - x, scale, bias, epsilon, mean, variance - ) - - saved_variance = 1 / np.sqrt(variance_tmp + epsilon) - - d_x, d_scale, d_bias = _reference_instance_norm_grad( - x, d_y, scale, saved_mean, saved_variance, epsilon - ) - - var_dict = locals() - var_dict['y@GRAD'] = d_y - var_dict['x@GRAD'] = d_x - var_dict['scale@GRAD'] = d_scale - var_dict['bias@GRAD'] = d_bias - - var_names = [ - 'x', - 'scale', - 'bias', - 'y', - 'saved_mean', - 'saved_variance', - ] - ground_truth = {name: var_dict[name] for name in var_names} - - program = base.Program() - with base.program_guard(program): - block = program.global_block() - for name in ground_truth: - block.create_var( - name=name, - dtype='float32', - shape=ground_truth[name].shape, - ) - in_op = block.append_op( - type="instance_norm", - inputs={ - "X": block.var("x"), - "Scale": block.var("scale"), - "Bias": block.var("bias"), - }, - outputs={ - "Y": block.var("y"), - "SavedMean": block.var("saved_mean"), - "SavedVariance": block.var("saved_variance"), - }, - attrs={ - "epsilon": epsilon, - }, - ) - - block.create_var(name="y@GRAD", dtype='float32', shape=y.shape) - - grad_op_desc_list, op_grad_to_var = core.get_grad_op_desc( - in_op.desc, self.no_grad_set, [] - ) - grad_op_desc = grad_op_desc_list[0] - new_op_desc = block.desc.append_op() - new_op_desc.copy_from(grad_op_desc) - for var_name in grad_op_desc.output_arg_names(): - block.desc.var(var_name.encode("ascii")) - grad_op_desc.infer_var_type(block.desc) - grad_op_desc.infer_shape(block.desc) - for arg in grad_op_desc.output_arg_names(): - grad_var = block.desc.find_var(arg.encode("ascii")) - grad_var.set_dtype(core.VarDesc.VarType.FP32) - - program._sync_with_cpp() - - exe = base.Executor(place) - out = exe.run( - program, - feed={ - name: var_dict[name] - for name in ['x', 'scale', 'bias', 'y@GRAD'] - }, - fetch_list=self.fetch_list, - ) - - for id, name in enumerate(self.fetch_list): - self.__assert_close(var_dict[name], out[id], name) - print("op test forward passes: ", str(place)) - paddle.disable_static() - - places = [core.CPUPlace()] - - if core.is_compiled_with_cuda() and core.op_support_gpu( - "instance_norm" - ): - places.append(core.CUDAPlace(0)) - for place in places: - test_with_place(place, self.shape) - - -class TestInstanceNormOpTrainingCase1(TestInstanceNormOpTraining): - def init_test_case(self): - self.shape = [2, 3, 4, 5] - self.no_grad_set = {'scale@GRAD', 'bias@GRAD'} - self.fetch_list = ['y', 'saved_mean', 'saved_variance', 'x@GRAD'] - - -class TestInstanceNormOpTrainingCase2(TestInstanceNormOpTraining): - def init_test_case(self): - self.shape = [20, 50, 4, 5] - self.no_grad_set = {'scale@GRAD', 'bias@GRAD'} - self.fetch_list = ['y', 'saved_mean', 'saved_variance', 'x@GRAD'] - - -class TestInstanceNormOpError(unittest.TestCase): - def test_errors(self): - paddle.enable_static() - with program_guard(Program(), Program()): - # the input of instance_norm must be Variable. - 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) - - # the input dtype of instance_norm must be float32 or float64 - x2 = paddle.static.data( - name='x2', shape=[-1, 3, 4, 5, 6], dtype="int32" - ) - self.assertRaises(TypeError, paddle.static.nn.instance_norm, x2) - paddle.disable_static() - - -class TestInstanceNormOpErrorCase1(unittest.TestCase): - def test_errors(self): - paddle.enable_static() - with program_guard(Program(), Program()): - # the first dimension of input for instance_norm must between [2d, 5d] - x = paddle.static.data(name='x', shape=[3], dtype="float32") - self.assertRaises(ValueError, paddle.static.nn.instance_norm, x) - paddle.disable_static() - - class TestElasticNormOp(unittest.TestCase): def init_test_case(self): self.epsilon = 1e-5 diff --git a/test/deprecated/legacy_test/test_instance_norm_op_deprecated.py b/test/deprecated/legacy_test/test_instance_norm_op_deprecated.py new file mode 100644 index 00000000000000..cc8e56b8be5e8b --- /dev/null +++ b/test/deprecated/legacy_test/test_instance_norm_op_deprecated.py @@ -0,0 +1,271 @@ +# 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. + +import unittest + +import numpy as np + +import paddle +from paddle import base +from paddle.base import Program, core, program_guard + + +def _reference_instance_norm_naive(x, scale, bias, epsilon, mean, var): + x_shape = x.shape + if len(x_shape) == 2: + x = np.reshape(x, (x.shape[0], x.shape[1], 1, 1)) + n, c, h, w = x.shape + + mean_tile = np.reshape(mean, (n, c, 1, 1)) + mean_tile = np.tile(mean_tile, (1, 1, h, w)) + var_tile = np.reshape(var, (n, c, 1, 1)) + var_tile = np.tile(var_tile, (1, 1, h, w)) + + x_norm = (x - mean_tile) / np.sqrt(var_tile + epsilon) + scale_tile = np.reshape(scale, (1, c, 1, 1)) + scale_tile = np.tile(scale_tile, (n, 1, h, w)) + bias_tile = np.reshape(bias, (1, c, 1, 1)) + bias_tile = np.tile(bias_tile, (n, 1, h, w)) + y = scale_tile * x_norm + bias_tile + if len(x_shape) == 2: + y = np.reshape(y, x_shape) + return y, mean, var + + +def _reference_instance_norm_grad(x, d_y, scale, mean, var, epsilon): + # d_scale = sum(d_y * (x-mean) / sqrt(var+epsilon)) + # d_offset = sum(d_y) + # d_x = scale / sqrt(var+epsilon) * (d_y - np.mean(d_y, axis=(2,3)) - (x-mean)/sqrt(var+epsilon)* np.mean(y_grad * (x-mean)/sqrt(var+epsilon), axis=(2,3))) + n, c, h, w = x.shape + + d_bias = np.sum(d_y, axis=(0, 2, 3)) + + mean_tile = np.reshape(mean, (n, c, 1, 1)) + mean_tile = np.tile(mean_tile, (1, 1, h, w)) + var_tile = np.reshape(var, (n, c, 1, 1)) + var_tile = np.tile(var_tile, (1, 1, h, w)) + + d_scale = np.sum(d_y * (x - mean_tile) * var_tile, axis=(0, 2, 3)) + var_inv = var_tile + scale_tile = np.reshape(scale, (1, c, 1, 1)) + scale_tile = np.tile(scale_tile, (n, 1, h, w)) + + d_x = ( + scale_tile + * var_inv + * ( + d_y + - np.mean(d_y, axis=(2, 3), keepdims=True) + - (x - mean_tile) + * var_inv + * np.mean( + d_y * (x - mean_tile) * var_inv, axis=(2, 3), keepdims=True + ) + ) + ) + return d_x, d_scale, d_bias + + +def _cal_mean_variance(x, epsilon, mean_shape): + mean = np.reshape(np.mean(x, axis=(2, 3)), mean_shape) + var = np.reshape(np.var(x, axis=(2, 3)), mean_shape) + return mean, var + + +def instance_norm_wrapper(x, weight=None, bias=None, esp=1e-05): + return paddle.nn.functional.instance_norm( + x, None, None, weight, bias, True, 0.9, esp + ) + + +class TestInstanceNormOpTraining(unittest.TestCase): + def setUp(self): + self.epsilon = 1e-5 + self.init_test_case() + + def init_test_case(self): + self.shape = [2, 3, 4, 5] + self.no_grad_set = set() + self.fetch_list = [ + 'y', + 'saved_mean', + 'saved_variance', + 'x@GRAD', + 'scale@GRAD', + 'bias@GRAD', + ] + + def __assert_close(self, tensor, np_array, msg, atol=1e-4): + np.testing.assert_allclose( + np.array(tensor), np_array, rtol=1e-05, atol=atol, err_msg=msg + ) + + def set_global_mean_var(self, mean_shape, x): + mean, variance = _cal_mean_variance(x, self.epsilon, mean_shape) + return mean, variance + + def test_forward_backward(self): + def test_with_place(place, shape): + paddle.enable_static() + epsilon = self.epsilon + n, c, h, w = shape[0], shape[1], shape[2], shape[3] + scale_shape = [c] + mean_shape = [n * c] + + np.random.seed() + x = np.random.random_sample(shape).astype(np.float32) + scale = np.random.random_sample(scale_shape).astype(np.float32) + bias = np.random.random_sample(scale_shape).astype(np.float32) + mean, variance = self.set_global_mean_var(mean_shape, x) + d_y = np.random.random_sample(shape).astype(np.float32) + + y, saved_mean, variance_tmp = _reference_instance_norm_naive( + x, scale, bias, epsilon, mean, variance + ) + + saved_variance = 1 / np.sqrt(variance_tmp + epsilon) + + d_x, d_scale, d_bias = _reference_instance_norm_grad( + x, d_y, scale, saved_mean, saved_variance, epsilon + ) + + var_dict = locals() + var_dict['y@GRAD'] = d_y + var_dict['x@GRAD'] = d_x + var_dict['scale@GRAD'] = d_scale + var_dict['bias@GRAD'] = d_bias + + var_names = [ + 'x', + 'scale', + 'bias', + 'y', + 'saved_mean', + 'saved_variance', + ] + ground_truth = {name: var_dict[name] for name in var_names} + + program = base.Program() + with base.program_guard(program): + block = program.global_block() + for name in ground_truth: + block.create_var( + name=name, + dtype='float32', + shape=ground_truth[name].shape, + ) + in_op = block.append_op( + type="instance_norm", + inputs={ + "X": block.var("x"), + "Scale": block.var("scale"), + "Bias": block.var("bias"), + }, + outputs={ + "Y": block.var("y"), + "SavedMean": block.var("saved_mean"), + "SavedVariance": block.var("saved_variance"), + }, + attrs={ + "epsilon": epsilon, + }, + ) + + block.create_var(name="y@GRAD", dtype='float32', shape=y.shape) + + grad_op_desc_list, op_grad_to_var = core.get_grad_op_desc( + in_op.desc, self.no_grad_set, [] + ) + grad_op_desc = grad_op_desc_list[0] + new_op_desc = block.desc.append_op() + new_op_desc.copy_from(grad_op_desc) + for var_name in grad_op_desc.output_arg_names(): + block.desc.var(var_name.encode("ascii")) + grad_op_desc.infer_var_type(block.desc) + grad_op_desc.infer_shape(block.desc) + for arg in grad_op_desc.output_arg_names(): + grad_var = block.desc.find_var(arg.encode("ascii")) + grad_var.set_dtype(core.VarDesc.VarType.FP32) + + program._sync_with_cpp() + + exe = base.Executor(place) + out = exe.run( + program, + feed={ + name: var_dict[name] + for name in ['x', 'scale', 'bias', 'y@GRAD'] + }, + fetch_list=self.fetch_list, + ) + + for id, name in enumerate(self.fetch_list): + self.__assert_close(var_dict[name], out[id], name) + print("op test forward passes: ", str(place)) + paddle.disable_static() + + places = [core.CPUPlace()] + + if core.is_compiled_with_cuda() and core.op_support_gpu( + "instance_norm" + ): + places.append(core.CUDAPlace(0)) + for place in places: + test_with_place(place, self.shape) + + +class TestInstanceNormOpTrainingCase1(TestInstanceNormOpTraining): + def init_test_case(self): + self.shape = [2, 3, 4, 5] + self.no_grad_set = {'scale@GRAD', 'bias@GRAD'} + self.fetch_list = ['y', 'saved_mean', 'saved_variance', 'x@GRAD'] + + +class TestInstanceNormOpTrainingCase2(TestInstanceNormOpTraining): + def init_test_case(self): + self.shape = [20, 50, 4, 5] + self.no_grad_set = {'scale@GRAD', 'bias@GRAD'} + self.fetch_list = ['y', 'saved_mean', 'saved_variance', 'x@GRAD'] + + +class TestInstanceNormOpError(unittest.TestCase): + def test_errors(self): + paddle.enable_static() + with program_guard(Program(), Program()): + # the input of instance_norm must be Variable. + 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) + + # the input dtype of instance_norm must be float32 or float64 + x2 = paddle.static.data( + name='x2', shape=[-1, 3, 4, 5, 6], dtype="int32" + ) + self.assertRaises(TypeError, paddle.static.nn.instance_norm, x2) + paddle.disable_static() + + +class TestInstanceNormOpErrorCase1(unittest.TestCase): + def test_errors(self): + paddle.enable_static() + with program_guard(Program(), Program()): + # the first dimension of input for instance_norm must between [2d, 5d] + x = paddle.static.data(name='x', shape=[3], dtype="float32") + self.assertRaises(ValueError, paddle.static.nn.instance_norm, x) + paddle.disable_static() + + +if __name__ == '__main__': + unittest.main() diff --git a/test/deprecated/legacy_test/test_layer_norm_op_deprecated.py b/test/deprecated/legacy_test/test_layer_norm_op_deprecated.py new file mode 100644 index 00000000000000..a4ab6d9e116c86 --- /dev/null +++ b/test/deprecated/legacy_test/test_layer_norm_op_deprecated.py @@ -0,0 +1,387 @@ +# Copyright (c) 2018 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 reduce +from operator import mul + +import numpy as np +from op_test import _set_use_system_allocator + +import paddle +from paddle import base +from paddle.base import core + +paddle.enable_static() + +np.random.seed(123) +paddle.seed(123) + +_set_use_system_allocator(True) + + +def _reference_layer_norm_naive(x, scale, beta, epsilon, begin_norm_axis=1): + x_shape = x.shape + N = reduce(mul, x_shape[0:begin_norm_axis], 1) + D = reduce(mul, x_shape[begin_norm_axis : len(x_shape)], 1) + x.shape = [N, D] + + mean = np.mean(x, axis=1) + var = np.var(x, axis=1) + epsilon + output = np.divide( + (x - mean.reshape([N, 1])), (np.sqrt(var)).reshape([N, 1]) + ) + if scale is not None: + output = scale.reshape([1, D]) * output + if beta is not None: + output = output + beta.reshape([1, D]) + + x.shape, output.shape = x_shape, x_shape + return output, mean, var + + +def _reference_layer_norm_grad( + x, grad_y, scale, bias, mean, var, begin_norm_axis=1 +): + x_shape = x.shape + N = reduce(mul, x_shape[0:begin_norm_axis], 1) + D = reduce(mul, x_shape[begin_norm_axis : len(x_shape)], 1) + + if scale is not None: + scale_shape = scale.shape + scale.shape = [1, D] + x.shape, grad_y.shape = [N, D], [N, D] + var.shape, mean.shape = [N, 1], [N, 1] + + # d_bias + if bias is not None: + d_bias = np.sum(grad_y, axis=0).reshape([1, D]) + else: + d_bias = None + # d_scale + if scale is not None: + d_scale = np.sum( + ((x - mean) * np.sqrt(1 / var)) * grad_y, axis=0 + ).reshape([1, D]) + else: + d_scale = None + # dx + if scale is not None: + dx_end = scale * np.sqrt(1.0 / var) * grad_y + d_mean_0 = np.sum(-np.sqrt(1.0 / var) * grad_y * scale, axis=1).reshape( + [N, 1] + ) # the second part equals to zero. + d_mean = 1.0 / D * d_mean_0 + d_std = np.sum( + -(1.0 / var) * (x - mean) * grad_y * scale, axis=1 + ).reshape([N, 1]) * ( + 1.0 / D * np.sqrt(1.0 / var).reshape([N, 1]) * (x - mean) + ) + else: + dx_end = 1.0 * np.sqrt(1.0 / var) * grad_y + d_mean_0 = np.sum(-np.sqrt(1.0 / var) * grad_y * 1.0, axis=1).reshape( + [N, 1] + ) # the second part equals to zero. + d_mean = 1.0 / D * d_mean_0 + d_std = np.sum( + -(1.0 / var) * (x - mean) * grad_y * 1.0, axis=1 + ).reshape([N, 1]) * ( + 1.0 / D * np.sqrt(1.0 / var).reshape([N, 1]) * (x - mean) + ) + + grad_x = dx_end + d_mean + d_std + + grad_x.shape, x.shape, grad_y.shape = x_shape, x_shape, x_shape + var.shape, mean.shape = [N], [N] + + if scale is not None: + scale.shape = scale_shape + return grad_x, d_scale, d_bias + + +def layer_norm_wrapper( + x, scale=None, bias=None, epsilon=1e-05, begin_norm_axis=1 +): + input_shape = list(x.shape) + normalized_shape = input_shape[begin_norm_axis:] + return paddle.nn.functional.layer_norm( + x, normalized_shape, weight=scale, bias=bias, epsilon=epsilon + ) + + +class TestLayerNormOp(unittest.TestCase): + def setUp(self): + self.use_cudnn = True + paddle.enable_static() + + def __assert_close(self, tensor, np_array, msg, atol=1e-4): + np.testing.assert_allclose( + np.array(tensor).flatten(), + np_array.flatten(), + rtol=1e-3, + atol=atol, + err_msg=msg, + ) + + def check_forward_backward( + self, + shape, + begin_norm_axis, + has_scale=True, + has_bias=True, + y_grad_scale=1.0, + use_mkldnn=False, + ): + def test_with_place( + place, shape, begin_norm_axis, use_mkldnn=use_mkldnn + ): + # attr + epsilon = 0.00001 + x_shape = shape + D = reduce(mul, x_shape[begin_norm_axis : len(x_shape)], 1) + scale_shape = [D] + + np.random.seed(123) + x = np.random.random_sample(x_shape).astype(np.float32) + scale = ( + np.random.random_sample(scale_shape).astype(np.float32) + if has_scale + else None + ) + bias = ( + np.random.random_sample(scale_shape).astype(np.float32) + if has_bias + else None + ) + y_grad = (np.random.random_sample(x_shape) * y_grad_scale).astype( + np.float32 + ) + + # reference forward & backward + y, mean, variance = _reference_layer_norm_naive( + x, scale, bias, epsilon, begin_norm_axis + ) + x_grad, scale_grad, bias_grad = _reference_layer_norm_grad( + x, y_grad, scale, bias, mean, variance, begin_norm_axis + ) + + var_dict = locals() + var_dict['y@GRAD'] = y_grad + var_names = ['x', 'mean', 'variance', 'y', 'y@GRAD'] + if has_scale: + var_names += ['scale'] + if has_bias: + var_names += ['bias'] + ground_truth = {name: var_dict[name] for name in var_names} + + program = base.Program() + with base.program_guard(program): + block = program.global_block() + for name in ground_truth: + block.create_var( + name=name, + dtype='float32', + shape=ground_truth[name].shape, + ) + inputs = {"X": block.var('x')} + fetch_list = [ + 'y', + 'mean', + 'variance', + 'x@GRAD', + ] + if has_scale: + inputs["Scale"] = block.var('scale') + fetch_list += ['scale@GRAD'] + if has_bias: + inputs["Bias"] = block.var('bias') + fetch_list += ['bias@GRAD'] + layer_norm_op = block.append_op( + type="layer_norm", + inputs=inputs, + outputs={ + "Y": block.var('y'), + "Mean": block.var('mean'), # share the same memory + "Variance": block.var( + 'variance' + ), # share the same memory + }, + attrs={ + "epsilon": epsilon, + "begin_norm_axis": begin_norm_axis, + "use_mkldnn": use_mkldnn, + }, + ) + # generate backward op_desc + grad_op_desc_list, op_grad_to_var = core.get_grad_op_desc( + layer_norm_op.desc, set(), [] + ) + grad_op_desc = grad_op_desc_list[0] + new_op_desc = block.desc.append_op() + new_op_desc.copy_from(grad_op_desc) + for var_name in grad_op_desc.output_arg_names(): + block.desc.var(var_name.encode("ascii")) + grad_op_desc.infer_var_type(block.desc) + grad_op_desc.infer_shape(block.desc) + for arg in grad_op_desc.output_arg_names(): + grad_var = block.desc.find_var(arg.encode("ascii")) + grad_var.set_dtype(core.VarDesc.VarType.FP32) + + program._sync_with_cpp() + exe = base.Executor(place) + name_list = ['x', 'y@GRAD'] + if has_scale: + name_list += ['scale'] + if has_bias: + name_list += ['bias'] + + out = exe.run( + program, + feed={name: var_dict[name] for name in name_list}, + fetch_list=fetch_list, + ) + # print(y) + # print(out[0]) + self.__assert_close(y, out[0], "y") + self.__assert_close(mean, out[1], "mean") + self.__assert_close(variance, out[2], "variance", 1e-3) + self.__assert_close(x_grad, out[3], "x_grad") + if has_scale: + self.__assert_close( + scale_grad, + out[fetch_list.index('scale@GRAD')], + "scale_grad", + 1e-3, + ) + if has_bias: + self.__assert_close( + bias_grad, + out[fetch_list.index('bias@GRAD')], + "bias_grad", + ) + + places = [core.CPUPlace()] + if ( + core.is_compiled_with_cuda() + and core.op_support_gpu("layer_norm") + and self.use_cudnn + ): + places.append(core.CUDAPlace(0)) + + for place in places: + test_with_place(place, shape, begin_norm_axis) + + def test_check_forward_backward_with_scale_and_bias(self): + self.check_forward_backward(shape=[2, 3, 4, 5], begin_norm_axis=1) + self.check_forward_backward(shape=[1, 3, 4, 5], begin_norm_axis=1) + self.check_forward_backward( + shape=[2, 3, 4, 5], + begin_norm_axis=1, + has_scale=False, + has_bias=True, + ) + self.check_forward_backward( + shape=[2, 3, 4, 5], + begin_norm_axis=1, + has_scale=True, + has_bias=False, + ) + self.check_forward_backward( + shape=[2, 3, 4, 5], + begin_norm_axis=1, + has_scale=False, + has_bias=False, + ) + self.check_forward_backward(shape=[2, 3, 4, 5], begin_norm_axis=3) + self.check_forward_backward( + shape=[92, 513, 129], begin_norm_axis=2, y_grad_scale=0.1 + ) + self.check_forward_backward(shape=[3, 34, 1134], begin_norm_axis=2) + self.check_forward_backward(shape=[3, 2, 1133], begin_norm_axis=2) + self.check_forward_backward( + shape=[92, 513, 1134], begin_norm_axis=2, y_grad_scale=0.1 + ) + self.check_forward_backward( + shape=[92, 513, 1134], + begin_norm_axis=2, + has_scale=False, + has_bias=True, + y_grad_scale=0.1, + ) + self.check_forward_backward( + shape=[92, 513, 1134], + begin_norm_axis=2, + has_scale=True, + has_bias=False, + y_grad_scale=0.1, + ) + self.check_forward_backward( + shape=[92, 513, 1134], + begin_norm_axis=2, + has_scale=False, + has_bias=False, + y_grad_scale=0.1, + ) + self.check_forward_backward( + shape=[512, 1024], begin_norm_axis=1, has_scale=True, has_bias=True + ) + self.check_forward_backward( + shape=[1, 128, 256, 256], + begin_norm_axis=3, + has_scale=True, + has_bias=True, + ) + self.check_forward_backward( + shape=[1, 256, 384], + begin_norm_axis=2, + has_scale=True, + has_bias=True, + ) + + +class TestLayerNormAPI(unittest.TestCase): + def test_case(self): + x = paddle.static.data(name='x', shape=[64, 32, 256], dtype='float32') + x = paddle.static.nn.layer_norm( + x, + scale=True, + shift=True, + begin_norm_axis=1, + epsilon=1e-05, + param_attr=None, + bias_attr=None, + ) + x = paddle.static.nn.layer_norm( + x, + scale=False, + shift=False, + begin_norm_axis=1, + epsilon=1e-05, + param_attr=None, + bias_attr=None, + ) + x = paddle.static.nn.layer_norm( + x, + scale=True, + shift=True, + begin_norm_axis=1, + epsilon=1e-05, + param_attr="scale", + bias_attr="shift", + ) + + +if __name__ == '__main__': + paddle.enable_static() + unittest.main() diff --git a/test/deprecated/legacy_test/test_math_op_patch.py b/test/deprecated/legacy_test/test_math_op_patch.py index fe0708098fb728..49331d3fc09557 100644 --- a/test/deprecated/legacy_test/test_math_op_patch.py +++ b/test/deprecated/legacy_test/test_math_op_patch.py @@ -19,8 +19,6 @@ import paddle from paddle import base -from paddle.framework import in_pir_mode -from paddle.pir_utils import test_with_pir_api class TestMathOpPatches(unittest.TestCase): @@ -232,32 +230,6 @@ def test_equal(self): np.testing.assert_array_equal(c_np, a_np == b_np) self.assertEqual(c.dtype, paddle.bool) - @prog_scope() - @test_with_pir_api - def test_equal_and_cond(self): - a = paddle.static.data(name="a", shape=[-1, 1], dtype='float32') - b = paddle.static.data(name="b", shape=[-1, 1], dtype='float32') - if not in_pir_mode(): - a.desc.set_need_check_feed(False) - b.desc.set_need_check_feed(False) - one = paddle.ones(shape=[1], dtype='int32') - zero = paddle.zeros(shape=[1], dtype='int32') - cond = one == zero - c = paddle.static.nn.cond(cond, lambda: a + b, lambda: a - b) - - 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( - paddle.static.default_main_program(), - feed={"a": a_np, "b": b_np}, - fetch_list=[c], - ) - - np.testing.assert_array_equal(c_np, a_np - b_np) - @prog_scope() def test_neg(self): a = paddle.static.data(name="a", shape=[-1, 10, 1], dtype='float32') diff --git a/test/deprecated/legacy_test/test_math_op_patch_deprecated.py b/test/deprecated/legacy_test/test_math_op_patch_deprecated.py new file mode 100644 index 00000000000000..982c439d99828b --- /dev/null +++ b/test/deprecated/legacy_test/test_math_op_patch_deprecated.py @@ -0,0 +1,60 @@ +# Copyright (c) 2018 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 decorator_helper import prog_scope + +import paddle +from paddle import base +from paddle.framework import in_pir_mode +from paddle.pir_utils import test_with_pir_api + + +class TestMathOpPatches(unittest.TestCase): + @classmethod + def setUp(self): + np.random.seed(1024) + paddle.enable_static() + + @prog_scope() + @test_with_pir_api + def test_equal_and_cond(self): + a = paddle.static.data(name="a", shape=[-1, 1], dtype='float32') + b = paddle.static.data(name="b", shape=[-1, 1], dtype='float32') + if not in_pir_mode(): + a.desc.set_need_check_feed(False) + b.desc.set_need_check_feed(False) + one = paddle.ones(shape=[1], dtype='int32') + zero = paddle.zeros(shape=[1], dtype='int32') + cond = one == zero + c = paddle.static.nn.cond(cond, lambda: a + b, lambda: a - b) + + 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( + paddle.static.default_main_program(), + feed={"a": a_np, "b": b_np}, + fetch_list=[c], + ) + + np.testing.assert_array_equal(c_np, a_np - b_np) + + +if __name__ == '__main__': + unittest.main() diff --git a/test/deprecated/legacy_test/test_merged_momentum_op.py b/test/deprecated/legacy_test/test_merged_momentum_op_deprecated.py similarity index 100% rename from test/deprecated/legacy_test/test_merged_momentum_op.py rename to test/deprecated/legacy_test/test_merged_momentum_op_deprecated.py diff --git a/test/deprecated/legacy_test/test_momentum_op.py b/test/deprecated/legacy_test/test_momentum_op.py index de8ba1886598e0..c48601326f4bd4 100644 --- a/test/deprecated/legacy_test/test_momentum_op.py +++ b/test/deprecated/legacy_test/test_momentum_op.py @@ -14,7 +14,6 @@ import unittest -import numpy import numpy as np from op import Operator from op_test import OpTest @@ -1035,80 +1034,6 @@ def test_main(self): self._check_with_param_group(place, use_amp) -class TestMultiTensorMomentumStatic(unittest.TestCase): - def _momentum_optimize_static( - self, place, use_amp=False, use_multi_tensor=False - ): - paddle.enable_static() - paddle.seed(10) - np.random.seed(10) - if place == 'cpu': - use_amp = False - exe = paddle.static.Executor(place=place) - train_program = paddle.static.Program() - startup_program = paddle.static.Program() - optimizer = paddle.optimizer.Momentum( - multi_precision=use_amp, use_multi_tensor=use_multi_tensor - ) - if use_amp: - optimizer = paddle.static.amp.decorate( - optimizer, - init_loss_scaling=128.0, - use_dynamic_loss_scaling=True, - use_pure_fp16=True, - use_fp16_guard=False, - ) - with paddle.static.program_guard(train_program, startup_program): - if use_amp: - data = paddle.static.data( - shape=[2, 2], name='X', dtype='float16' - ) - else: - data = paddle.static.data( - shape=[2, 2], name='X', dtype='float32' - ) - hidden = paddle.static.nn.fc(x=data, size=10) - loss = paddle.mean(hidden) - optimizer.minimize(loss) - exe.run(startup_program) - if use_amp: - optimizer.amp_init( - place=paddle.CUDAPlace(0), scope=paddle.static.global_scope() - ) - x = numpy.random.random(size=(2, 2)).astype('float16') - else: - x = numpy.random.random(size=(2, 2)).astype('float32') - out = [] - for idx in range(5): - (loss_data,) = exe.run( - train_program, feed={"X": x}, fetch_list=[loss] - ) - out.append(loss_data) - return out - - def _get_places(self): - places = ['cpu'] - if paddle.is_compiled_with_cuda(): - places.append('gpu') - return places - - def _check_with_place_amp(self, place, use_amp): - output1 = self._momentum_optimize_static( - place=place, use_amp=use_amp, use_multi_tensor=True - ) - output2 = self._momentum_optimize_static( - place=place, use_amp=use_amp, use_multi_tensor=False - ) - for idx in range(len(output1)): - np.testing.assert_allclose(output1[idx], output2[idx], rtol=1e-05) - - def test_main(self): - for place in self._get_places(): - use_amp_list = [True, False] - for use_amp in use_amp_list: - self._check_with_place_amp(place, use_amp) - - if __name__ == "__main__": paddle.enable_static() unittest.main() diff --git a/test/deprecated/legacy_test/test_momentum_op_deprecated.py b/test/deprecated/legacy_test/test_momentum_op_deprecated.py new file mode 100644 index 00000000000000..65c5e584d0c5ff --- /dev/null +++ b/test/deprecated/legacy_test/test_momentum_op_deprecated.py @@ -0,0 +1,157 @@ +# Copyright (c) 2018 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 +import numpy as np + +import paddle + + +def calculate_momentum_by_numpy( + param, + grad, + mu, + velocity, + use_nesterov, + learning_rate, + regularization_method=None, + regularization_coeff=1.0, +): + if regularization_method == "l2_decay": + grad = grad + regularization_coeff * param + + velocity_out = mu * velocity + grad + if use_nesterov: + param_out = param - (grad + velocity_out * mu) * learning_rate + else: + param_out = param - learning_rate * velocity_out + else: + velocity_out = mu * velocity + grad + if use_nesterov: + param_out = ( + param - grad * learning_rate - velocity_out * mu * learning_rate + ) + else: + param_out = param - learning_rate * velocity_out + + return param_out, velocity_out + + +def momentum_wrapper( + param, + grad, + velocity, + learning_rate=1.0, + master_param=None, + mu=0.0, + use_nesterov=False, + regularization_method="", + regularization_coeff=0.0, + multi_precision=False, + rescale_grad=1.0, +): + return paddle._C_ops.momentum_( + param, + grad, + velocity, + learning_rate, + master_param, + mu, + use_nesterov, + regularization_method, + regularization_coeff, + multi_precision, + rescale_grad, + ) + + +class TestMultiTensorMomentumStatic(unittest.TestCase): + def _momentum_optimize_static( + self, place, use_amp=False, use_multi_tensor=False + ): + paddle.enable_static() + paddle.seed(10) + np.random.seed(10) + if place == 'cpu': + use_amp = False + exe = paddle.static.Executor(place=place) + train_program = paddle.static.Program() + startup_program = paddle.static.Program() + optimizer = paddle.optimizer.Momentum( + multi_precision=use_amp, use_multi_tensor=use_multi_tensor + ) + if use_amp: + optimizer = paddle.static.amp.decorate( + optimizer, + init_loss_scaling=128.0, + use_dynamic_loss_scaling=True, + use_pure_fp16=True, + use_fp16_guard=False, + ) + with paddle.static.program_guard(train_program, startup_program): + if use_amp: + data = paddle.static.data( + shape=[2, 2], name='X', dtype='float16' + ) + else: + data = paddle.static.data( + shape=[2, 2], name='X', dtype='float32' + ) + hidden = paddle.static.nn.fc(x=data, size=10) + loss = paddle.mean(hidden) + optimizer.minimize(loss) + exe.run(startup_program) + if use_amp: + optimizer.amp_init( + place=paddle.CUDAPlace(0), scope=paddle.static.global_scope() + ) + x = numpy.random.random(size=(2, 2)).astype('float16') + else: + x = numpy.random.random(size=(2, 2)).astype('float32') + out = [] + for idx in range(5): + (loss_data,) = exe.run( + train_program, feed={"X": x}, fetch_list=[loss] + ) + out.append(loss_data) + return out + + def _get_places(self): + places = ['cpu'] + if paddle.is_compiled_with_cuda(): + places.append('gpu') + return places + + def _check_with_place_amp(self, place, use_amp): + output1 = self._momentum_optimize_static( + place=place, use_amp=use_amp, use_multi_tensor=True + ) + output2 = self._momentum_optimize_static( + place=place, use_amp=use_amp, use_multi_tensor=False + ) + for idx in range(len(output1)): + np.testing.assert_allclose(output1[idx], output2[idx], rtol=1e-05) + + def test_main(self): + for place in self._get_places(): + use_amp_list = [True, False] + for use_amp in use_amp_list: + self._check_with_place_amp(place, use_amp) + + +if __name__ == "__main__": + paddle.enable_static() + unittest.main() diff --git a/test/deprecated/legacy_test/test_name_scope.py b/test/deprecated/legacy_test/test_name_scope_deprecated.py similarity index 98% rename from test/deprecated/legacy_test/test_name_scope.py rename to test/deprecated/legacy_test/test_name_scope_deprecated.py index 4b3e5dd0ff9df7..e0822313ef27ad 100644 --- a/test/deprecated/legacy_test/test_name_scope.py +++ b/test/deprecated/legacy_test/test_name_scope_deprecated.py @@ -17,6 +17,8 @@ import paddle from paddle import base +paddle.enable_static() + class TestNameScope(unittest.TestCase): def test_name_scope(self): diff --git a/test/deprecated/legacy_test/test_nce.py b/test/deprecated/legacy_test/test_nce_deprecated.py similarity index 79% rename from test/deprecated/legacy_test/test_nce.py rename to test/deprecated/legacy_test/test_nce_deprecated.py index 1091f706d1935d..fbfea5a4359cd1 100644 --- a/test/deprecated/legacy_test/test_nce.py +++ b/test/deprecated/legacy_test/test_nce_deprecated.py @@ -15,7 +15,7 @@ import unittest import numpy as np -from op_test import OpTest, paddle_static_guard +from op_test import paddle_static_guard import paddle from paddle import base @@ -66,94 +66,6 @@ def nce( ) -class TestNCE(OpTest): - def generate_data( - self, - dim, - batch_size, - num_classes, - num_true_class, - num_neg_samples, - is_sparse, - ): - input = np.random.randn(batch_size, dim).astype(np.float32) - weight = np.random.randn(num_classes, dim).astype(np.float32) - bias = np.random.randn(num_classes).astype(np.float32) - sample_weight = np.random.randn(batch_size).astype(np.float32) - labels = np.random.randint( - 0, num_classes, (batch_size, num_true_class) - ).astype("int64") - self.attrs = { - 'num_total_classes': num_classes, - 'num_neg_samples': num_neg_samples, - 'custom_neg_classes': list(range(num_neg_samples)), - 'seed': 0, - 'sampler': 0, - 'is_sparse': is_sparse, - 'is_test': self.is_test, - } - self.inputs = { - 'Input': input, - 'Label': labels, - 'Weight': weight, - 'Bias': bias, - 'SampleWeight': sample_weight, - } - - def set_is_test(self): - self.is_test = False - - def set_data(self): - self.generate_data(5, 25, 100, 1, 2, False) - - def compute(self): - out = nce( - self.inputs['Input'], - self.inputs['Weight'], - self.inputs['Bias'], - self.inputs['SampleWeight'], - self.inputs['Label'], - self.attrs['num_total_classes'], - self.attrs['num_neg_samples'], - ) - if self.is_test: - self.outputs = {'Cost': out[0]} - else: - self.outputs = { - 'Cost': out[0], - 'SampleLogits': out[1], - 'SampleLabels': out[2], - } - - def setUp(self): - self.op_type = 'nce' - self.set_is_test() - self.set_data() - self.compute() - - def test_check_output(self): - self.check_output() - - def test_check_grad(self): - self.check_grad( - ["Input", "Weight", "Bias"], "Cost", max_relative_error=0.02 - ) - - -class TestNCECase1Tensor(TestNCE): - def set_data(self): - self.generate_data(10, 20, 100, 2, 5, False) - - -class TestNCETensorIsTest(TestNCE): - # if is_test = True, there's no need to calculate grad - def set_is_test(self): - self.is_test = True - - def test_check_grad(self): - pass - - class TestNCECase1SelectedRows(unittest.TestCase): def setUp(self): self.base_lr = 0.0001 diff --git a/test/deprecated/legacy_test/test_optimizer.py b/test/deprecated/legacy_test/test_optimizer_deprecated.py similarity index 88% rename from test/deprecated/legacy_test/test_optimizer.py rename to test/deprecated/legacy_test/test_optimizer_deprecated.py index c7e6d211241768..b3aa2e6c5f932f 100644 --- a/test/deprecated/legacy_test/test_optimizer.py +++ b/test/deprecated/legacy_test/test_optimizer_deprecated.py @@ -12,11 +12,8 @@ # See the License for the specific language governing permissions and # limitations under the License. -import os -import tempfile import unittest -import numpy import numpy as np import paddle @@ -25,10 +22,8 @@ from paddle.base.backward import append_backward from paddle.base.framework import ( Program, - convert_np_dtype_to_dtype_, program_guard, ) -from paddle.io import Dataset class TestOptimizer(unittest.TestCase): @@ -1012,142 +1007,6 @@ def test_program_desc( ) -class TestOptimizerDtype(unittest.TestCase): - ''' - The dtype of optimizer should be inferred by parameters, and the learning rate - is cteated with the same dtype. - ''' - - def check_with_dtype(self, dtype): - class MyLayer(paddle.nn.Layer): - def __init__(self, dtype): - super().__init__() - self._w = self.create_parameter([2, 3], dtype=dtype) - self._b = self.create_parameter([2, 3], dtype=dtype) - - def forward(self, x): - return x * self._w + self._b - - with paddle.base.dygraph.guard(): - model = MyLayer(dtype) - x = paddle.rand([10, 2, 3], dtype=dtype) - loss = model(x) - adam = paddle.optimizer.Adam(parameters=model.parameters()) - loss.backward() - adam.step() - self.assertEqual(adam._dtype, convert_np_dtype_to_dtype_(dtype)) - - def test_float64(self): - self.check_with_dtype('float64') - - def test_float32(self): - self.check_with_dtype('float32') - - -@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 TestMasterWeightSaveForFP16(unittest.TestCase): - ''' - For Amp-O2, some optimizer(Momentum, Adam ...) will create master weights for parameters to improve the accuracy. - Master weights will be saved by optimizer::state_dict. - ''' - - def setUp(self): - self.temp_dir = tempfile.TemporaryDirectory() - - def tearDown(self): - self.temp_dir.cleanup() - - def check_with_opt_state_dict(self, use_save_load=True): - paddle.seed(100) - numpy.random.seed(100) - - class SimpleNet(paddle.nn.Layer): - def __init__(self, input_size, output_size): - super().__init__() - self.linears = paddle.nn.LayerList( - [ - paddle.nn.Linear(input_size, output_size) - for i in range(1) - ] - ) - - def forward(self, x): - for i, l in enumerate(self.linears): - x = self.linears[i](x) - return x - - input_size = 2 # 设为较大的值 - output_size = 2 # 设为较大的值 - batch_size = 2 # batch_size 为8的倍数 - nums_batch = 10 - - class RandomDataset(Dataset): - def __init__(self, num_samples): - self.num_samples = num_samples - - def __getitem__(self, idx): - data = numpy.random.random([input_size]).astype('float16') - label = numpy.random.random([output_size]).astype('float16') - return data, label - - def __len__(self): - return self.num_samples - - dataset = RandomDataset(nums_batch * batch_size) - loader = paddle.io.DataLoader( - dataset, - batch_size=batch_size, - shuffle=False, - drop_last=True, - num_workers=0, - ) - - mse = paddle.nn.MSELoss() - model = SimpleNet(input_size, output_size) # 定义模型 - optimizer = paddle.optimizer.Momentum( - learning_rate=0.0001, - parameters=model.parameters(), - multi_precision=True, - ) # 定义优化器 - scaler = paddle.amp.GradScaler(init_loss_scaling=1024) - model = paddle.amp.decorate(models=model, level='O2') - - for i, (data, label) in enumerate(loader): - with paddle.amp.auto_cast(level='O2'): - output = model(data) - loss = mse(output, label) - scaled = scaler.scale(loss) - scaled.backward() - scaler.step(optimizer) - scaler.update() - optimizer.clear_grad(set_to_zero=False) - - if use_save_load and i == 5: - model_path = os.path.join(self.temp_dir.name, "model.pdparams") - optimizer_path = os.path.join(self.temp_dir.name, "opt.pdopt") - paddle.save(model.state_dict(), model_path) - paddle.save(optimizer.state_dict(), optimizer_path) - model.set_state_dict(paddle.load(model_path)) - optimizer.set_state_dict(paddle.load(optimizer_path)) - - return loss.numpy() - - def test_with_state_dict(self): - if core.is_compiled_with_cuda(): - with base.dygraph.guard(): - out_use_state_dict = self.check_with_opt_state_dict( - use_save_load=True - ) - out_no_state_dict = self.check_with_opt_state_dict( - use_save_load=False - ) - np.testing.assert_array_equal(out_use_state_dict, out_no_state_dict) - - if __name__ == '__main__': paddle.enable_static() unittest.main() diff --git a/test/deprecated/legacy_test/test_optimizer_in_control_flow.py b/test/deprecated/legacy_test/test_optimizer_in_control_flow_deprecated.py similarity index 100% rename from test/deprecated/legacy_test/test_optimizer_in_control_flow.py rename to test/deprecated/legacy_test/test_optimizer_in_control_flow_deprecated.py diff --git a/test/deprecated/legacy_test/test_prelu_op_deprecated.py b/test/deprecated/legacy_test/test_prelu_op_deprecated.py new file mode 100644 index 00000000000000..f329a58ecd15f0 --- /dev/null +++ b/test/deprecated/legacy_test/test_prelu_op_deprecated.py @@ -0,0 +1,86 @@ +# Copyright (c) 2018 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 base +from paddle.base import Program, core + +paddle.enable_static() + + +def prelu_t(x, mode, param_attr=None, name=None, data_format='NCHW'): + 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( + attr=helper.param_attr, + shape=alpha_shape, + dtype='float32', + is_bias=False, + default_initializer=paddle.nn.initializer.Constant(0.25), + ) + out = helper.create_variable_for_type_inference(dtype) + helper.append_op( + type="prelu", + inputs={"X": x, 'Alpha': alpha}, + attrs={"mode": mode, 'data_format': data_format}, + outputs={"Out": out}, + ) + return out + + +# error message test if mode is not one of 'all', 'channel', 'element' +class TestModeError(unittest.TestCase): + def setUp(self): + self.place = ( + paddle.CUDAPlace(0) + if core.is_compiled_with_cuda() + else paddle.CPUPlace() + ) + self.x_np = np.ones([1, 2, 3, 4]).astype('float32') + + def test_mode_error(self): + 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') + except Exception as e: + assert e.args[0].find('InvalidArgument') != -1 + + def test_data_format_error1(self): + 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') + except Exception as e: + assert e.args[0].find('InvalidArgument') != -1 + + def test_data_format_error2(self): + 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') + except ValueError as e: + pass + + +if __name__ == "__main__": + unittest.main() diff --git a/test/deprecated/legacy_test/test_program_code.py b/test/deprecated/legacy_test/test_program_code_deprecated.py similarity index 100% rename from test/deprecated/legacy_test/test_program_code.py rename to test/deprecated/legacy_test/test_program_code_deprecated.py diff --git a/test/deprecated/legacy_test/test_program.py b/test/deprecated/legacy_test/test_program_deprecated.py similarity index 100% rename from test/deprecated/legacy_test/test_program.py rename to test/deprecated/legacy_test/test_program_deprecated.py diff --git a/test/deprecated/legacy_test/test_program_prune_backward.py b/test/deprecated/legacy_test/test_program_prune_backward_deprecated.py similarity index 100% rename from test/deprecated/legacy_test/test_program_prune_backward.py rename to test/deprecated/legacy_test/test_program_prune_backward_deprecated.py diff --git a/test/deprecated/legacy_test/test_program_to_string.py b/test/deprecated/legacy_test/test_program_to_string_deprecated.py similarity index 98% rename from test/deprecated/legacy_test/test_program_to_string.py rename to test/deprecated/legacy_test/test_program_to_string_deprecated.py index c6524d9cf5d926..52768d46007853 100644 --- a/test/deprecated/legacy_test/test_program_to_string.py +++ b/test/deprecated/legacy_test/test_program_to_string_deprecated.py @@ -17,6 +17,8 @@ import paddle from paddle import base +paddle.enable_static() + class TestProgram(unittest.TestCase): def test_program_to_string(self): diff --git a/test/deprecated/legacy_test/test_prune.py b/test/deprecated/legacy_test/test_prune_deprecated.py similarity index 99% rename from test/deprecated/legacy_test/test_prune.py rename to test/deprecated/legacy_test/test_prune_deprecated.py index f82a4d4331b099..47f0d3d7497019 100644 --- a/test/deprecated/legacy_test/test_prune.py +++ b/test/deprecated/legacy_test/test_prune_deprecated.py @@ -21,6 +21,8 @@ from paddle import base from paddle.base import framework +paddle.enable_static() + class TestPruneBase(unittest.TestCase): def run_net(self, net): diff --git a/test/deprecated/legacy_test/test_py_func_op.py b/test/deprecated/legacy_test/test_py_func_op_deprecated.py similarity index 99% rename from test/deprecated/legacy_test/test_py_func_op.py rename to test/deprecated/legacy_test/test_py_func_op_deprecated.py index 3fa249935406fc..89ad64aa7d4abd 100644 --- a/test/deprecated/legacy_test/test_py_func_op.py +++ b/test/deprecated/legacy_test/test_py_func_op_deprecated.py @@ -20,6 +20,8 @@ import paddle from paddle import base +paddle.enable_static() + dev_cnt = 2 if base.core.is_compiled_with_cuda(): dev_cnt = base.core.get_cuda_device_count() diff --git a/test/deprecated/legacy_test/test_random_seed_deprecated.py b/test/deprecated/legacy_test/test_random_seed_deprecated.py new file mode 100644 index 00000000000000..ee1dd64b81ee34 --- /dev/null +++ b/test/deprecated/legacy_test/test_random_seed_deprecated.py @@ -0,0 +1,82 @@ +# Copyright (c) 2018 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. +"""Test cloud role maker.""" + +import unittest + +import numpy as np + +import paddle +from paddle import base +from paddle.base import core + + +class TestGeneratorSeed(unittest.TestCase): + # """ + # Test cases for cpu generator seed. + # """ + def test_gen_TruncatedNormal_initializer(self): + base.disable_dygraph() + + gen = paddle.seed(123123143) + cur_state = gen.get_state() + + 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]) + result_1 = paddle.static.nn.fc( + x, + size=10, + weight_attr=paddle.nn.initializer.TruncatedNormal( + mean=0.0, std=2.0 + ), + ) + result_2 = paddle.static.nn.fc( + x, + size=10, + weight_attr=paddle.nn.initializer.TruncatedNormal( + mean=0.0, std=2.0 + ), + ) + + 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 base.program_guard(train_program, startup_program): + exe.run(startup_program) + out2 = exe.run( + train_program, feed={}, fetch_list=[result_1, result_2] + ) + + out1_res1 = np.array(out1[0]) + out1_res2 = np.array(out1[1]) + out2_res1 = np.array(out2[0]) + out2_res2 = np.array(out2[1]) + + if not core.is_compiled_with_cuda(): + print(">>>>>>> sampling id static >>>>>>>") + np.testing.assert_allclose(out1_res1, out2_res1, rtol=1e-05) + np.testing.assert_allclose(out1_res2, out2_res2, rtol=1e-05) + self.assertTrue(not np.allclose(out1_res2, out1_res1)) + + +if __name__ == "__main__": + unittest.main() diff --git a/test/deprecated/legacy_test/test_regularizer_api_deprecated.py b/test/deprecated/legacy_test/test_regularizer_api_deprecated.py new file mode 100644 index 00000000000000..f42e07d3ae0ccb --- /dev/null +++ b/test/deprecated/legacy_test/test_regularizer_api_deprecated.py @@ -0,0 +1,180 @@ +# Copyright (c) 2018 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 contextlib +import random +import unittest +from functools import partial + +import numpy as np + +import paddle +from paddle import base +from paddle.base import core + + +def bow_net( + data, + label, + dict_dim, + is_sparse=False, + emb_dim=8, + hid_dim=8, + hid_dim2=6, + class_dim=2, +): + """ + BOW net + This model is from https://github.com/PaddlePaddle/models: + base/PaddleNLP/text_classification/nets.py + """ + emb = paddle.static.nn.embedding( + input=data, is_sparse=is_sparse, size=[dict_dim, emb_dim] + ) + bow = paddle.static.nn.sequence_lod.sequence_pool( + input=emb, pool_type='sum' + ) + bow_tanh = paddle.tanh(bow) + fc_1 = paddle.static.nn.fc(x=bow_tanh, size=hid_dim, activation="tanh") + fc_2 = paddle.static.nn.fc(x=fc_1, size=hid_dim2, activation="tanh") + prediction = paddle.static.nn.fc( + x=[fc_2], size=class_dim, activation="softmax" + ) + cost = paddle.nn.functional.cross_entropy( + input=prediction, label=label, reduction='none', use_softmax=False + ) + avg_cost = paddle.mean(x=cost) + + return avg_cost + + +class TestRegularizer(unittest.TestCase): + def setUp(self): + self.word_len = 1500 + self.train_data = [ + [(random.sample(range(1000), 10), [0])] for _ in range(2) + ] + + def get_places(self): + places = [core.CPUPlace()] + if core.is_compiled_with_cuda(): + places.append(core.CUDAPlace(0)) + return places + + @contextlib.contextmanager + def scope_prog_guard(self, 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 = base.Executor(place) + feeder = base.DataFeeder(feed_list=feed_list, place=place) + exe.run(base.default_startup_program()) + + main_prog = base.default_main_program() + param_list = [var.name for var in main_prog.block(0).all_parameters()] + + param_sum = [] + for data in self.train_data: + out = exe.run( + main_prog, feed=feeder.feed(data), fetch_list=param_list + ) + p_sum = 0 + for v in out: + p_sum += np.sum(np.abs(v)) + param_sum.append(p_sum) + return param_sum + + def check_l2decay_regularizer(self, place, model): + paddle.seed(1) + paddle.framework.random._manual_program_seed(1) + main_prog = paddle.static.Program() + startup_prog = paddle.static.Program() + with self.scope_prog_guard( + main_prog=main_prog, startup_prog=startup_prog + ): + data = paddle.static.data( + name="words", shape=[-1, 1], dtype="int64", lod_level=1 + ) + label = paddle.static.data( + name="label", shape=[-1, 1], dtype="int64" + ) + + avg_cost = model(data, label, self.word_len) + + optimizer = paddle.optimizer.Adagrad( + learning_rate=0.1, + weight_decay=paddle.regularizer.L2Decay(1.0), + ) + optimizer.minimize(avg_cost) + param_sum = self.run_program(place, [data, label]) + return param_sum + + def check_l2decay(self, place, model): + paddle.seed(1) + paddle.framework.random._manual_program_seed(1) + main_prog = base.framework.Program() + startup_prog = base.framework.Program() + + with self.scope_prog_guard( + main_prog=main_prog, startup_prog=startup_prog + ): + data = paddle.static.data( + name="words", shape=[-1, 1], dtype="int64", lod_level=1 + ) + label = paddle.static.data( + name="label", shape=[-1, 1], dtype="int64" + ) + + avg_cost_l2 = model(data, label, self.word_len) + + param_list = base.default_main_program().block(0).all_parameters() + para_sum = [] + for para in param_list: + para_mul = paddle.square(x=para) + para_sum.append(paddle.sum(para_mul)) + avg_cost_l2 += paddle.add_n(para_sum) * 0.5 + + optimizer = paddle.optimizer.Adagrad(learning_rate=0.1) + optimizer.minimize(avg_cost_l2) + param_sum = self.run_program(place, [data, label]) + return param_sum + + def test_l2(self): + paddle.enable_static() + for place in self.get_places(): + dense_sparse_p_sum = [] + for sparse in [True, False]: + model = partial(bow_net, is_sparse=sparse) + framework_l2 = self.check_l2decay_regularizer(place, model) + l2 = self.check_l2decay(place, model) + assert len(l2) == len(framework_l2) + for i in range(len(l2)): + assert np.isclose(a=framework_l2[i], b=l2[i], rtol=5e-5) + dense_sparse_p_sum.append(framework_l2) + + assert len(dense_sparse_p_sum[0]) == len(dense_sparse_p_sum[1]) + for i in range(len(dense_sparse_p_sum[0])): + assert np.isclose( + a=dense_sparse_p_sum[0][i], + b=dense_sparse_p_sum[1][i], + rtol=5e-5, + ) + + +if __name__ == '__main__': + unittest.main() diff --git a/test/deprecated/legacy_test/test_regularizer.py b/test/deprecated/legacy_test/test_regularizer_deprecated.py similarity index 70% rename from test/deprecated/legacy_test/test_regularizer.py rename to test/deprecated/legacy_test/test_regularizer_deprecated.py index 8a3cd3da9a0490..1727fe8b4f5d0c 100644 --- a/test/deprecated/legacy_test/test_regularizer.py +++ b/test/deprecated/legacy_test/test_regularizer_deprecated.py @@ -23,7 +23,6 @@ from paddle import base, regularizer from paddle.base import core, framework from paddle.base.backward import append_backward -from paddle.pir_utils import test_with_pir_api class TestL2Decay(unittest.TestCase): @@ -112,40 +111,6 @@ def test_l2decay_regularizer(self): self.assertEqual(block.ops[-2].type, 'scale') self.assertEqual(block.ops[-3].type, 'sign') - def test_l1decay_regularizer(self): - with paddle.pir_utils.IrGuard(): - main_program = paddle.static.Program() - with paddle.static.program_guard(main_program): - block = main_program.global_block() - mul_x = paddle.pir.core.create_parameter( - dtype="float32", - shape=[5, 10], - name="mul.x", - regularizer=regularizer.L1Decay(0.5), - initializer=paddle.nn.initializer.Constant(1), - ) - self.assertIsNotNone(mul_x.regularizer) - self.assertTrue( - isinstance(mul_x.regularizer, regularizer.L1Decay) - ) - - mul_y = paddle.static.data( - dtype="float32", shape=[10, 8], name="mul.y" - ) - mul_out = paddle.matmul(mul_x, mul_y) - mean_out = paddle.mean(mul_out) - grads = paddle.autograd.ir_backward.grad(mean_out, [mul_x]) - params_grads = [(mul_x, grads[0])] - self.assertEqual(len(params_grads), 1) - count_ops = len(block.ops) - optimizer = paddle.optimizer.Adam() - params_grads = optimizer.append_regularization_ops(params_grads) - self.assertEqual(len(params_grads), 1) - self.assertEqual(len(block.ops), count_ops + 5) - self.assertEqual(block.ops[-1].name(), 'pd_op.add_n') - self.assertEqual(block.ops[-3].name(), 'pd_op.scale') - self.assertEqual(block.ops[-5].name(), 'pd_op.sign') - def bow_net( data, @@ -296,63 +261,6 @@ def test_l2(self): rtol=5e-5, ) - @test_with_pir_api - def test_repeated_regularization(self): - l1 = paddle.regularizer.L1Decay(coeff=0.1) - l2 = paddle.regularizer.L2Decay(coeff=0.01) - fc_param_attr = paddle.ParamAttr( - regularizer=paddle.regularizer.L1Decay() - ) - with paddle.static.program_guard( - paddle.static.Program(), paddle.static.Program() - ): - x = paddle.uniform([2, 2, 3]) - linear = paddle.nn.Linear(3, 5, weight_attr=fc_param_attr) - out = linear(x) - loss = paddle.sum(out) - sgd = paddle.optimizer.SGD(learning_rate=0.1, weight_decay=l2) - sgd.minimize(loss) - with base.dygraph.guard(): - input = paddle.to_tensor(np.random.randn(3, 2).astype('float32')) - paddle.seed(1) - paddle.framework.random._manual_program_seed(1) - - linear1 = paddle.nn.Linear( - 2, 2, weight_attr=fc_param_attr, bias_attr=fc_param_attr - ) - linear2 = paddle.nn.Linear( - 2, 2, weight_attr=fc_param_attr, bias_attr=fc_param_attr - ) - - loss1 = linear1(input) - loss1.backward() - # 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 base.ParamAttr - loss2 = linear2(input) - loss2.backward() - paddle.optimizer.SGD( - parameters=linear2.parameters(), learning_rate=1e-2 - ).minimize(loss2) - # they should both be applied by l1, and keep the same - np.testing.assert_allclose( - linear1.weight.numpy(), - linear2.weight.numpy(), - rtol=1e-05, - 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 base.ParamAttr!', - ) - if __name__ == '__main__': unittest.main() diff --git a/test/deprecated/legacy_test/test_run_program_op.py b/test/deprecated/legacy_test/test_run_program_op_deprecated.py similarity index 100% rename from test/deprecated/legacy_test/test_run_program_op.py rename to test/deprecated/legacy_test/test_run_program_op_deprecated.py diff --git a/test/deprecated/legacy_test/test_select_input_output_op.py b/test/deprecated/legacy_test/test_select_input_output_op_deprecated.py similarity index 100% rename from test/deprecated/legacy_test/test_select_input_output_op.py rename to test/deprecated/legacy_test/test_select_input_output_op_deprecated.py diff --git a/test/deprecated/legacy_test/test_sgd_op_deprecated.py b/test/deprecated/legacy_test/test_sgd_op_deprecated.py new file mode 100644 index 00000000000000..11d899f755526e --- /dev/null +++ b/test/deprecated/legacy_test/test_sgd_op_deprecated.py @@ -0,0 +1,214 @@ +# Copyright (c) 2018 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 base + +paddle.enable_static() + + +def sgd_wrapper( + param, learning_rate, grad, master_param=None, multi_precision=False +): + paddle._C_ops.sgd_( + param, learning_rate, grad, master_param, multi_precision + ) + + +class TestSGDOpWithLargeInput(unittest.TestCase): + def runTest(self): + paddle.enable_static() + data = paddle.tensor.fill_constant(shape=[1], value=128, dtype='int64') + label = paddle.tensor.fill_constant( + shape=[1, 150], value=0.5, dtype='float32' + ) + emb = paddle.static.nn.embedding( + input=data, size=(10000000, 150), dtype='float32' + ) + out = paddle.nn.functional.normalize(x=emb, axis=-1) + + cost = paddle.nn.functional.square_error_cost(input=out, label=label) + avg_cost = paddle.mean(cost) + sgd_optimizer = paddle.optimizer.SGD(learning_rate=0.001) + sgd_optimizer.minimize(avg_cost) + + 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]) + + +class TestSGDV2(unittest.TestCase): + def test_sgd(self): + paddle.enable_static() + + def check_sgd_optimizer(optimizer_attr): + init_program = paddle.static.Program() + program = paddle.static.Program() + block = program.global_block() + mul_x = block.create_parameter( + dtype="float32", + shape=[5, 10], + lod_level=0, + name="mul.x", + optimize_attr=optimizer_attr, + ) + mul_y = block.create_var( + dtype="float32", shape=[10, 8], lod_level=0, name="mul.y" + ) + mul_out = block.create_var( + dtype="float32", shape=[5, 8], lod_level=0, name="mul.out" + ) + mean_out = block.create_var( + dtype="float32", shape=[1], lod_level=0, name="mean.out" + ) + block.append_op( + type="mul", + inputs={"X": mul_x, "Y": mul_y}, + outputs={"Out": mul_out}, + attrs={"x_num_col_dims": 1}, + ) + block.append_op( + type="mean", inputs={"X": mul_out}, outputs={"Out": mean_out} + ) + sgd_optimizer = paddle.optimizer.SGD(learning_rate=0.01) + opts, _ = sgd_optimizer.minimize(mean_out, init_program) + return opts + + opts = check_sgd_optimizer({'learning_rate': 1.1}) + self.assertEqual(len(opts), 2) + self.assertEqual([op.type for op in opts], ["scale", "sgd"]) + + opts = check_sgd_optimizer({'learning_rate': 1.0}) + self.assertEqual(len(opts), 1) + self.assertEqual([op.type for op in opts], ["sgd"]) + + +class TestSGDMultiPrecision2_0(unittest.TestCase): + def dygraph_sgd_mp(self, mp): + paddle.disable_static() + paddle.seed(10) + paddle.set_device('gpu') + input = paddle.randn((2, 2)) + model = paddle.nn.Linear(2, 2) + optimizer = paddle.optimizer.SGD( + parameters=model.parameters(), multi_precision=mp + ) + if mp: + model = paddle.amp.decorate(models=model, level='O2') + scaler = paddle.amp.GradScaler(init_loss_scaling=1024) + + for idx in range(5): + if mp: + with paddle.amp.auto_cast(level='O2'): + output = model(input) + loss = paddle.mean(output) + scaled = scaler.scale(loss) + scaled.backward() + scaler.minimize(optimizer, scaled) + optimizer.clear_grad() + else: + output = model(input) + loss = paddle.mean(output) + optimizer.step() + optimizer.clear_grad() + + return output, model.parameters() + + def static_sgd_mp(self, mp): + paddle.enable_static() + paddle.seed(10) + np.random.seed(10) + exe = paddle.static.Executor('gpu') + train_program = paddle.static.Program() + startup_program = paddle.static.Program() + optimizer = paddle.optimizer.SGD(multi_precision=mp) + + if mp: + optimizer = paddle.static.amp.decorate( + optimizer, + init_loss_scaling=128.0, + use_dynamic_loss_scaling=True, + use_pure_fp16=True, + use_fp16_guard=False, + ) + with paddle.static.program_guard(train_program, startup_program): + if mp: + data = paddle.static.data( + shape=[2, 2], name='X', dtype='float16' + ) + else: + data = paddle.static.data( + shape=[2, 2], name='X', dtype='float32' + ) + hidden = paddle.static.nn.fc(x=data, size=10) + loss = paddle.mean(hidden) + optimizer.minimize(loss) + exe.run(startup_program) + + if mp: + optimizer.amp_init( + place=paddle.CUDAPlace(0), scope=paddle.static.global_scope() + ) + x = np.random.random(size=(2, 2)).astype('float16') + else: + x = np.random.random(size=(2, 2)).astype('float32') + out = [] + for idx in range(5): + (loss_data,) = exe.run( + train_program, feed={"X": x}, fetch_list=[loss] + ) + out.append(loss_data) + return out + + def test_main(self): + if not paddle.is_compiled_with_cuda(): + return + "Test dygraph mode" + output1_dy, params1_dy = self.dygraph_sgd_mp(mp=True) + output2_dy, params2_dy = self.dygraph_sgd_mp(mp=False) + np.testing.assert_allclose( + output1_dy.astype('float32').numpy(), + output2_dy.astype('float32').numpy(), + rtol=1e-05, + atol=0.1, + ) + for idx in range(len(params1_dy)): + np.testing.assert_allclose( + params1_dy[idx].astype('float32').numpy(), + params2_dy[idx].astype('float32').numpy(), + rtol=1e-05, + atol=0.1, + ) + "Test static graph mode" + output1_st = self.static_sgd_mp(mp=True) + output2_st = self.static_sgd_mp(mp=False) + for idx in range(len(output1_st)): + np.testing.assert_allclose( + output1_st[idx].astype('float32'), + output2_st[idx].astype('float32'), + rtol=1e-05, + atol=0.1, + ) + + +if __name__ == "__main__": + unittest.main() diff --git a/test/deprecated/legacy_test/test_slice_op.py b/test/deprecated/legacy_test/test_slice_op.py index 3a91882cac51e4..1040eb676952ad 100644 --- a/test/deprecated/legacy_test/test_slice_op.py +++ b/test/deprecated/legacy_test/test_slice_op.py @@ -1011,15 +1011,6 @@ def test_float_in_index(): class TestInferShape(unittest.TestCase): - def test(self): - with paddle_static_guard(): - x = paddle.ones(shape=[3, 4, 5]) - x.desc.set_shape([3, -1, 5]) - self.assertEqual(x.shape, (3, -1, 5)) - - out0 = paddle.slice(x, axes=[1], starts=[0], ends=[3]) - self.assertEqual(out0.shape, (3, -1, 5)) - def test_pir(self): with paddle.pir_utils.IrGuard(): x = paddle.static.data('x', shape=[3, -1, 5]) diff --git a/test/deprecated/legacy_test/test_slice_op_deprecated.py b/test/deprecated/legacy_test/test_slice_op_deprecated.py new file mode 100644 index 00000000000000..a9ba98f3dba728 --- /dev/null +++ b/test/deprecated/legacy_test/test_slice_op_deprecated.py @@ -0,0 +1,37 @@ +# Copyright (c) 2018 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 op_test import paddle_static_guard + +import paddle + +paddle.enable_static() + + +class TestInferShape(unittest.TestCase): + def test(self): + with paddle_static_guard(): + x = paddle.ones(shape=[3, 4, 5]) + x.desc.set_shape([3, -1, 5]) + self.assertEqual(x.shape, (3, -1, 5)) + + out0 = paddle.slice(x, axes=[1], starts=[0], ends=[3]) + self.assertEqual(out0.shape, (3, -1, 5)) + + +if __name__ == '__main__': + paddle.enable_static() + unittest.main() diff --git a/test/deprecated/legacy_test/test_static_pylayer_block.py b/test/deprecated/legacy_test/test_static_pylayer_block_deprecated.py similarity index 100% rename from test/deprecated/legacy_test/test_static_pylayer_block.py rename to test/deprecated/legacy_test/test_static_pylayer_block_deprecated.py diff --git a/test/deprecated/legacy_test/test_static_pylayer.py b/test/deprecated/legacy_test/test_static_pylayer_deprecated.py similarity index 99% rename from test/deprecated/legacy_test/test_static_pylayer.py rename to test/deprecated/legacy_test/test_static_pylayer_deprecated.py index ec0e655d3b6a9a..f6db7f054cc16d 100644 --- a/test/deprecated/legacy_test/test_static_pylayer.py +++ b/test/deprecated/legacy_test/test_static_pylayer_deprecated.py @@ -14,9 +14,12 @@ import functools import unittest +from test.deprecated.legacy_test.test_prune_deprecated import ( + TestExecutorRunAutoPrune, + TestPruneBase, +) import numpy as np -from test_prune import TestExecutorRunAutoPrune, TestPruneBase import paddle from paddle import base diff --git a/test/deprecated/legacy_test/test_switch.py b/test/deprecated/legacy_test/test_switch_deprecated.py similarity index 99% rename from test/deprecated/legacy_test/test_switch.py rename to test/deprecated/legacy_test/test_switch_deprecated.py index 3c90ba52605428..d8b2e2fd061ad9 100644 --- a/test/deprecated/legacy_test/test_switch.py +++ b/test/deprecated/legacy_test/test_switch_deprecated.py @@ -19,6 +19,8 @@ from paddle.base.executor import Executor from paddle.base.framework import default_startup_program +paddle.enable_static() + class TestSwitch(unittest.TestCase): def check_switch(self, value): diff --git a/test/deprecated/legacy_test/test_tensor_array_to_tensor.py b/test/deprecated/legacy_test/test_tensor_array_to_tensor.py index 91310fc2880fb5..7d043ad52dac74 100644 --- a/test/deprecated/legacy_test/test_tensor_array_to_tensor.py +++ b/test/deprecated/legacy_test/test_tensor_array_to_tensor.py @@ -43,122 +43,6 @@ def test_list_Variable(): self.assertRaises(TypeError, test_list_Variable) -class TestLoDTensorArrayConcat(unittest.TestCase): - """Test case for concat mode of tensor_array_to_tensor.""" - - def setUp(self): - self.op_type = "tensor_array_to_tensor" - self.attrs = {"axis": 0} - self.outputs = ["Out"] - - def test_get_set(self): - scope = core.Scope() - program = base.Program() - block = program.global_block() - - input_arr = block.create_var( - name="tmp_lod_tensor_array", - type=core.VarDesc.VarType.LOD_TENSOR_ARRAY, - ) - input_arr.persistable = True - input_arr_var = scope.var('tmp_lod_tensor_array') - input_tensor_array = input_arr_var.get_lod_tensor_array() - self.assertEqual(0, len(input_tensor_array)) - - cpu = core.CPUPlace() - for i in range(10): - t = core.LoDTensor() - if i == 0: - t.set(np.array([[i], [i]], dtype='float32'), cpu) - else: - t.set(np.array([[i]], dtype='float32'), cpu) - input_tensor_array.append(t) - - self.assertEqual(10, len(input_tensor_array)) - - random_grad = np.random.random_sample([11]).astype(np.float32) - - y_out = block.create_var(name="Out") - y_out.persistable = True - y_out_index = block.create_var(name="OutIndex") - y_out_index.persistable = True - - y_grad_arr = block.create_var( - name='Out@GRAD', dtype='float32', shape=[11] - ) - y_grad_arr.persistable = True - y_grad = scope.var('Out@GRAD') - y_grad_tensor = y_grad.get_tensor() - y_grad_tensor.set(random_grad, cpu) - - op = block.append_op( - type=self.op_type, - inputs={"X": input_arr}, - outputs={"Out": y_out, "OutIndex": y_out_index}, - attrs=self.attrs, - ) - - out_grad = block.create_var( - name="tmp_lod_tensor_array@GRAD", - type=core.VarDesc.VarType.LOD_TENSOR_ARRAY, - ) - out_grad.persistable = True - - grad_op_desc_list, op_grad_to_var = core.get_grad_op_desc( - op.desc, set(), [] - ) - grad_op_desc = grad_op_desc_list[0] - new_op_desc = block.desc.append_op() - new_op_desc.copy_from(grad_op_desc) - for var_name in grad_op_desc.output_arg_names(): - block.desc.var(var_name.encode("ascii")) - - grad_op_desc.infer_var_type(block.desc) - grad_op_desc.infer_shape(block.desc) - for arg in grad_op_desc.output_arg_names(): - grad_var = block.desc.find_var(arg.encode("ascii")) - grad_var.set_dtype(core.VarDesc.VarType.FP32) - - fetch_list = [] - fetch_list.append(block.var('Out')) - fetch_list.append(block.var('OutIndex')) - - exe = base.Executor(base.CPUPlace()) - out = exe.run(program, fetch_list=fetch_list, scope=scope) - # print ("index: ", np.array(out[1])) - - # test forward - tensor_res = np.array(out[0]) - tensor_gt = np.array( - [0] + [0, 1, 2, 3, 4, 5, 6, 7, 8, 9], dtype='float32' - ) - - self.assertEqual(len(tensor_res), len(tensor_gt)) - - for i in range(len(tensor_res)): - self.assertEqual(tensor_res[i], tensor_gt[i]) - - # test backward - grad_tensor = scope.var('tmp_lod_tensor_array@GRAD') - grad_tensor_array = grad_tensor.get_lod_tensor_array() - - self.assertEqual(10, len(grad_tensor_array)) - - for i in range(len(grad_tensor_array)): - if i == 0: - self.assertEqual( - np.array(grad_tensor_array[i])[0], np.array(random_grad[i]) - ) - self.assertEqual( - np.array(grad_tensor_array[i])[1], - np.array(random_grad[i + 1]), - ) - if i == 1: - self.assertEqual( - np.array(grad_tensor_array[i]), np.array(random_grad[i + 1]) - ) - - class TestLoDTensorArrayStack(unittest.TestCase): """Test case for stack mode of tensor_array_to_tensor.""" diff --git a/test/deprecated/legacy_test/test_tensor_array_to_tensor_deprecated.py b/test/deprecated/legacy_test/test_tensor_array_to_tensor_deprecated.py new file mode 100644 index 00000000000000..959eddee79fe69 --- /dev/null +++ b/test/deprecated/legacy_test/test_tensor_array_to_tensor_deprecated.py @@ -0,0 +1,143 @@ +# Copyright (c) 2018 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 base +from paddle.base import core + +paddle.enable_static() + + +class TestLoDTensorArrayConcat(unittest.TestCase): + """Test case for concat mode of tensor_array_to_tensor.""" + + def setUp(self): + self.op_type = "tensor_array_to_tensor" + self.attrs = {"axis": 0} + self.outputs = ["Out"] + + def test_get_set(self): + scope = core.Scope() + program = base.Program() + block = program.global_block() + + input_arr = block.create_var( + name="tmp_lod_tensor_array", + type=core.VarDesc.VarType.LOD_TENSOR_ARRAY, + ) + input_arr.persistable = True + input_arr_var = scope.var('tmp_lod_tensor_array') + input_tensor_array = input_arr_var.get_lod_tensor_array() + self.assertEqual(0, len(input_tensor_array)) + + cpu = core.CPUPlace() + for i in range(10): + t = core.LoDTensor() + if i == 0: + t.set(np.array([[i], [i]], dtype='float32'), cpu) + else: + t.set(np.array([[i]], dtype='float32'), cpu) + input_tensor_array.append(t) + + self.assertEqual(10, len(input_tensor_array)) + + random_grad = np.random.random_sample([11]).astype(np.float32) + + y_out = block.create_var(name="Out") + y_out.persistable = True + y_out_index = block.create_var(name="OutIndex") + y_out_index.persistable = True + + y_grad_arr = block.create_var( + name='Out@GRAD', dtype='float32', shape=[11] + ) + y_grad_arr.persistable = True + y_grad = scope.var('Out@GRAD') + y_grad_tensor = y_grad.get_tensor() + y_grad_tensor.set(random_grad, cpu) + + op = block.append_op( + type=self.op_type, + inputs={"X": input_arr}, + outputs={"Out": y_out, "OutIndex": y_out_index}, + attrs=self.attrs, + ) + + out_grad = block.create_var( + name="tmp_lod_tensor_array@GRAD", + type=core.VarDesc.VarType.LOD_TENSOR_ARRAY, + ) + out_grad.persistable = True + + grad_op_desc_list, op_grad_to_var = core.get_grad_op_desc( + op.desc, set(), [] + ) + grad_op_desc = grad_op_desc_list[0] + new_op_desc = block.desc.append_op() + new_op_desc.copy_from(grad_op_desc) + for var_name in grad_op_desc.output_arg_names(): + block.desc.var(var_name.encode("ascii")) + + grad_op_desc.infer_var_type(block.desc) + grad_op_desc.infer_shape(block.desc) + for arg in grad_op_desc.output_arg_names(): + grad_var = block.desc.find_var(arg.encode("ascii")) + grad_var.set_dtype(core.VarDesc.VarType.FP32) + + fetch_list = [] + fetch_list.append(block.var('Out')) + fetch_list.append(block.var('OutIndex')) + + exe = base.Executor(base.CPUPlace()) + out = exe.run(program, fetch_list=fetch_list, scope=scope) + # print ("index: ", np.array(out[1])) + + # test forward + tensor_res = np.array(out[0]) + tensor_gt = np.array( + [0] + [0, 1, 2, 3, 4, 5, 6, 7, 8, 9], dtype='float32' + ) + + self.assertEqual(len(tensor_res), len(tensor_gt)) + + for i in range(len(tensor_res)): + self.assertEqual(tensor_res[i], tensor_gt[i]) + + # test backward + grad_tensor = scope.var('tmp_lod_tensor_array@GRAD') + grad_tensor_array = grad_tensor.get_lod_tensor_array() + + self.assertEqual(10, len(grad_tensor_array)) + + for i in range(len(grad_tensor_array)): + if i == 0: + self.assertEqual( + np.array(grad_tensor_array[i])[0], np.array(random_grad[i]) + ) + self.assertEqual( + np.array(grad_tensor_array[i])[1], + np.array(random_grad[i + 1]), + ) + if i == 1: + self.assertEqual( + np.array(grad_tensor_array[i]), np.array(random_grad[i + 1]) + ) + + +if __name__ == '__main__': + unittest.main() diff --git a/test/deprecated/legacy_test/test_trainable.py b/test/deprecated/legacy_test/test_trainable_deprecated.py similarity index 100% rename from test/deprecated/legacy_test/test_trainable.py rename to test/deprecated/legacy_test/test_trainable_deprecated.py diff --git a/test/deprecated/legacy_test/test_truncated_gaussian_random_op.py b/test/deprecated/legacy_test/test_truncated_gaussian_random_op_deprecated.py similarity index 100% rename from test/deprecated/legacy_test/test_truncated_gaussian_random_op.py rename to test/deprecated/legacy_test/test_truncated_gaussian_random_op_deprecated.py diff --git a/test/deprecated/legacy_test/test_variable.py b/test/deprecated/legacy_test/test_variable.py index cbe52f6ef103ee..5b33f4c6a4cc85 100644 --- a/test/deprecated/legacy_test/test_variable.py +++ b/test/deprecated/legacy_test/test_variable.py @@ -112,125 +112,6 @@ def test_step_scopes(self): ) self.assertEqual(core.VarDesc.VarType.STEP_SCOPES, var.type) - def _test_slice(self, place): - b = default_main_program().current_block() - w = b.create_var(dtype="float64", shape=[784, 100, 100], lod_level=0) - - for i in range(3): - nw = w[i] - self.assertEqual((100, 100), nw.shape) - - nw = w[:] - self.assertEqual((784, 100, 100), nw.shape) - - nw = w[:, :] - self.assertEqual((784, 100, 100), nw.shape) - - nw = w[:, :, -1] - self.assertEqual((784, 100), nw.shape) - - nw = w[1, 1, 1] - - self.assertEqual(len(nw.shape), 0) - - nw = w[:, :, :-1] - self.assertEqual((784, 100, 99), nw.shape) - - self.assertEqual(0, nw.lod_level) - - 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]], - [[10, 11, 12], [13, 14, 15], [16, 17, 18]], - [[19, 20, 21], [22, 23, 24], [25, 26, 27]], - ] - ).astype('float32') - var = paddle.assign(tensor_array) - var1 = var[0, 1, 1] - var2 = var[1:] - var3 = var[0:1] - var4 = var[::-1] - var5 = var[1, 1:, 1:] - var_reshape = paddle.reshape(var, [3, -1, 3]) - var6 = var_reshape[:, :, -1] - var7 = var[:, :, :-1] - var8 = var[:1, :1, :1] - var9 = var[:-1, :-1, :-1] - var10 = var[::-1, :1, :-1] - var11 = var[:-1, ::-1, -1:] - var12 = var[1:2, 2:, ::-1] - var13 = var[2:10, 2:, -2:-1] - var14 = var[1:-1, 0:2, ::-1] - var15 = var[::-1, ::-1, ::-1] - - 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 = base.DataFeeder(place=place, feed_list=[x]) - data = [] - data.append(np.random.randint(10, size=[13]).astype('float32')) - exe.run(base.default_startup_program()) - - local_out = exe.run( - main, - feed=feeder.feed([data]), - fetch_list=[ - var, - var1, - var2, - var3, - var4, - var5, - var6, - var7, - var8, - var9, - var10, - var11, - var12, - var13, - var14, - var15, - ], - ) - - np.testing.assert_array_equal(local_out[1], tensor_array[0, 1, 1:2]) - np.testing.assert_array_equal(local_out[2], tensor_array[1:]) - np.testing.assert_array_equal(local_out[3], tensor_array[0:1]) - np.testing.assert_array_equal(local_out[4], tensor_array[::-1]) - np.testing.assert_array_equal(local_out[5], tensor_array[1, 1:, 1:]) - np.testing.assert_array_equal( - local_out[6], tensor_array.reshape((3, -1, 3))[:, :, -1] - ) - np.testing.assert_array_equal(local_out[7], tensor_array[:, :, :-1]) - np.testing.assert_array_equal( - local_out[8], tensor_array[:1, :1, :1] - ) - np.testing.assert_array_equal( - local_out[9], tensor_array[:-1, :-1, :-1] - ) - np.testing.assert_array_equal( - local_out[10], tensor_array[::-1, :1, :-1] - ) - np.testing.assert_array_equal( - local_out[11], tensor_array[:-1, ::-1, -1:] - ) - np.testing.assert_array_equal( - local_out[12], tensor_array[1:2, 2:, ::-1] - ) - np.testing.assert_array_equal( - local_out[13], tensor_array[2:10, 2:, -2:-1] - ) - np.testing.assert_array_equal( - local_out[14], tensor_array[1:-1, 0:2, ::-1] - ) - np.testing.assert_array_equal( - local_out[15], tensor_array[::-1, ::-1, ::-1] - ) - def _test_slice_index_tensor(self, place): data = np.random.rand(2, 3).astype("float32") prog = paddle.static.Program() @@ -391,7 +272,6 @@ def test_slice(self): places.append(core.CUDAPlace(0)) for place in places: - self._test_slice(place) self._test_slice_index_tensor(place) self._test_slice_index_list(place) self._test_slice_index_ellipsis(place) diff --git a/test/deprecated/legacy_test/test_variable_deprecated.py b/test/deprecated/legacy_test/test_variable_deprecated.py new file mode 100644 index 00000000000000..6d416d1a20344c --- /dev/null +++ b/test/deprecated/legacy_test/test_variable_deprecated.py @@ -0,0 +1,162 @@ +# Copyright (c) 2018 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 base +from paddle.base import core +from paddle.base.framework import ( + default_main_program, +) + +paddle.enable_static() + + +class TestVariable(unittest.TestCase): + def setUp(self): + np.random.seed(2022) + + def _test_slice(self, place): + b = default_main_program().current_block() + w = b.create_var(dtype="float64", shape=[784, 100, 100], lod_level=0) + + for i in range(3): + nw = w[i] + self.assertEqual((100, 100), nw.shape) + + nw = w[:] + self.assertEqual((784, 100, 100), nw.shape) + + nw = w[:, :] + self.assertEqual((784, 100, 100), nw.shape) + + nw = w[:, :, -1] + self.assertEqual((784, 100), nw.shape) + + nw = w[1, 1, 1] + + self.assertEqual(len(nw.shape), 0) + + nw = w[:, :, :-1] + self.assertEqual((784, 100, 99), nw.shape) + + self.assertEqual(0, nw.lod_level) + + 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]], + [[10, 11, 12], [13, 14, 15], [16, 17, 18]], + [[19, 20, 21], [22, 23, 24], [25, 26, 27]], + ] + ).astype('float32') + var = paddle.assign(tensor_array) + var1 = var[0, 1, 1] + var2 = var[1:] + var3 = var[0:1] + var4 = var[::-1] + var5 = var[1, 1:, 1:] + var_reshape = paddle.reshape(var, [3, -1, 3]) + var6 = var_reshape[:, :, -1] + var7 = var[:, :, :-1] + var8 = var[:1, :1, :1] + var9 = var[:-1, :-1, :-1] + var10 = var[::-1, :1, :-1] + var11 = var[:-1, ::-1, -1:] + var12 = var[1:2, 2:, ::-1] + var13 = var[2:10, 2:, -2:-1] + var14 = var[1:-1, 0:2, ::-1] + var15 = var[::-1, ::-1, ::-1] + + 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 = base.DataFeeder(place=place, feed_list=[x]) + data = [] + data.append(np.random.randint(10, size=[13]).astype('float32')) + exe.run(base.default_startup_program()) + + local_out = exe.run( + main, + feed=feeder.feed([data]), + fetch_list=[ + var, + var1, + var2, + var3, + var4, + var5, + var6, + var7, + var8, + var9, + var10, + var11, + var12, + var13, + var14, + var15, + ], + ) + + np.testing.assert_array_equal(local_out[1], tensor_array[0, 1, 1:2]) + np.testing.assert_array_equal(local_out[2], tensor_array[1:]) + np.testing.assert_array_equal(local_out[3], tensor_array[0:1]) + np.testing.assert_array_equal(local_out[4], tensor_array[::-1]) + np.testing.assert_array_equal(local_out[5], tensor_array[1, 1:, 1:]) + np.testing.assert_array_equal( + local_out[6], tensor_array.reshape((3, -1, 3))[:, :, -1] + ) + np.testing.assert_array_equal(local_out[7], tensor_array[:, :, :-1]) + np.testing.assert_array_equal( + local_out[8], tensor_array[:1, :1, :1] + ) + np.testing.assert_array_equal( + local_out[9], tensor_array[:-1, :-1, :-1] + ) + np.testing.assert_array_equal( + local_out[10], tensor_array[::-1, :1, :-1] + ) + np.testing.assert_array_equal( + local_out[11], tensor_array[:-1, ::-1, -1:] + ) + np.testing.assert_array_equal( + local_out[12], tensor_array[1:2, 2:, ::-1] + ) + np.testing.assert_array_equal( + local_out[13], tensor_array[2:10, 2:, -2:-1] + ) + np.testing.assert_array_equal( + local_out[14], tensor_array[1:-1, 0:2, ::-1] + ) + np.testing.assert_array_equal( + local_out[15], tensor_array[::-1, ::-1, ::-1] + ) + + def test_slice(self): + places = [base.CPUPlace()] + if core.is_compiled_with_cuda(): + places.append(core.CUDAPlace(0)) + + for place in places: + self._test_slice(place) + + +if __name__ == '__main__': + unittest.main() diff --git a/test/deprecated/legacy_test/test_weight_normalization.py b/test/deprecated/legacy_test/test_weight_normalization_deprecated.py similarity index 99% rename from test/deprecated/legacy_test/test_weight_normalization.py rename to test/deprecated/legacy_test/test_weight_normalization_deprecated.py index f8793aef3052e3..6e799166b4d42c 100644 --- a/test/deprecated/legacy_test/test_weight_normalization.py +++ b/test/deprecated/legacy_test/test_weight_normalization_deprecated.py @@ -22,6 +22,8 @@ from paddle.base import core from paddle.base.param_attr import WeightNormParamAttr +paddle.enable_static() + class TestWeightNormalization(unittest.TestCase): batch_size = 3 diff --git a/test/deprecated/prim/composite_ops/test_composite_layer_norm.py b/test/deprecated/prim/composite_ops/test_composite_layer_norm_deprecated.py similarity index 100% rename from test/deprecated/prim/composite_ops/test_composite_layer_norm.py rename to test/deprecated/prim/composite_ops/test_composite_layer_norm_deprecated.py diff --git a/test/deprecated/prim/test_comp_get_grad_op_desc_prim_enabled.py b/test/deprecated/prim/test_comp_get_grad_op_desc_prim_enabled_deprecated.py similarity index 100% rename from test/deprecated/prim/test_comp_get_grad_op_desc_prim_enabled.py rename to test/deprecated/prim/test_comp_get_grad_op_desc_prim_enabled_deprecated.py diff --git a/test/deprecated/prim/test_comp_skip_op_set.py b/test/deprecated/prim/test_comp_skip_op_set_deprecated.py similarity index 100% rename from test/deprecated/prim/test_comp_skip_op_set.py rename to test/deprecated/prim/test_comp_skip_op_set_deprecated.py diff --git a/test/deprecated/quantization/CMakeLists.txt b/test/deprecated/quantization/CMakeLists.txt index 5fc3911d0417f0..a5fc7d59e42f87 100644 --- a/test/deprecated/quantization/CMakeLists.txt +++ b/test/deprecated/quantization/CMakeLists.txt @@ -193,10 +193,13 @@ if(WIN32) list(REMOVE_ITEM TEST_OPS test_imperative_qat_amp) list(REMOVE_ITEM TEST_OPS test_weight_only_linear) list(REMOVE_ITEM TEST_OPS test_llm_int8_linear) - list(REMOVE_ITEM TEST_OPS test_quant_aware) + list(REMOVE_ITEM TEST_OPS test_quant_aware_deprecated) list(REMOVE_ITEM TEST_OPS test_quant_post_quant_aware) + list(REMOVE_ITEM TEST_OPS test_quant_post_quant_aware_deprecated) list(REMOVE_ITEM TEST_OPS test_quant_aware_user_defined) + list(REMOVE_ITEM TEST_OPS test_quant_aware_user_defined_deprecated) list(REMOVE_ITEM TEST_OPS test_quant_amp) + list(REMOVE_ITEM TEST_OPS test_quant_amp_deprecated) list(REMOVE_ITEM TEST_OPS test_apply_per_channel_scale) endif() @@ -237,13 +240,16 @@ list(REMOVE_ITEM TEST_OPS test_filter_pruning) if(WIN32) set(SINGLE_CARD_TEST_OPS test_user_defined_quantization + test_user_defined_quantization_deprecated test_quantization_scale_pass + test_quantization_scale_pass_deprecated test_quantization_pass - test_moving_average_abs_max_scale_op + test_quantization_pass_deprecated + test_moving_average_abs_max_scale_op_deprecated test_imperative_qat_channelwise test_imperative_qat test_imperative_out_scale - test_graph) + test_graph_deprecated) list(REMOVE_ITEM TEST_OPS ${SINGLE_CARD_TEST_OPS}) foreach(src ${SINGLE_CARD_TEST_OPS}) py_test(${src} SRCS ${src}.py ENVS CUDA_VISIBLE_DEVICES=0) @@ -260,16 +266,24 @@ if(NOT WIN32) 120) set_tests_properties(test_weight_quantization_mobilenetv1 PROPERTIES TIMEOUT 120) - set_tests_properties(test_quant_aware PROPERTIES TIMEOUT 200) + set_tests_properties(test_quant_aware_deprecated PROPERTIES TIMEOUT 200) set_tests_properties(test_quant_post_quant_aware PROPERTIES TIMEOUT 200) + set_tests_properties(test_quant_post_quant_aware_deprecated PROPERTIES TIMEOUT + 200) set_tests_properties(test_quant_aware_user_defined PROPERTIES TIMEOUT 200) + set_tests_properties(test_quant_aware_user_defined_deprecated + PROPERTIES TIMEOUT 200) set_tests_properties(test_quant_amp PROPERTIES TIMEOUT 200) + set_tests_properties(test_quant_amp_deprecated PROPERTIES TIMEOUT 200) endif() -set_tests_properties(test_graph PROPERTIES TIMEOUT 120) +set_tests_properties(test_graph_deprecated PROPERTIES TIMEOUT 120) set_tests_properties(test_quantization_pass PROPERTIES TIMEOUT 120) +set_tests_properties(test_quantization_pass_deprecated PROPERTIES TIMEOUT 120) set_tests_properties(test_imperative_qat_channelwise PROPERTIES TIMEOUT 200) set_tests_properties(test_user_defined_quantization PROPERTIES TIMEOUT 200) +set_tests_properties(test_user_defined_quantization_deprecated + PROPERTIES TIMEOUT 200) set_tests_properties(test_imperative_qat PROPERTIES TIMEOUT 200) set_tests_properties(test_imperative_qat_fuse PROPERTIES TIMEOUT 200) set_tests_properties(test_imperative_out_scale PROPERTIES TIMEOUT 200) diff --git a/test/deprecated/quantization/test_graph.py b/test/deprecated/quantization/test_graph_deprecated.py similarity index 100% rename from test/deprecated/quantization/test_graph.py rename to test/deprecated/quantization/test_graph_deprecated.py diff --git a/test/deprecated/quantization/test_moving_average_abs_max_scale_op.py b/test/deprecated/quantization/test_moving_average_abs_max_scale_op_deprecated.py similarity index 100% rename from test/deprecated/quantization/test_moving_average_abs_max_scale_op.py rename to test/deprecated/quantization/test_moving_average_abs_max_scale_op_deprecated.py diff --git a/test/deprecated/quantization/test_quant2_int8_mkldnn_pass.py b/test/deprecated/quantization/test_quant2_int8_mkldnn_pass_deprecated.py similarity index 100% rename from test/deprecated/quantization/test_quant2_int8_mkldnn_pass.py rename to test/deprecated/quantization/test_quant2_int8_mkldnn_pass_deprecated.py diff --git a/test/deprecated/quantization/test_quant_amp.py b/test/deprecated/quantization/test_quant_amp.py index 2f285dfdf07d93..ac0f411f39855b 100644 --- a/test/deprecated/quantization/test_quant_amp.py +++ b/test/deprecated/quantization/test_quant_amp.py @@ -16,9 +16,9 @@ import logging import os import unittest +from test.deprecated.quantization.test_quant_aware_deprecated import MobileNet import numpy as np -from test_quant_aware import MobileNet import paddle from paddle.static.quantization.quanter import convert, quant_aware diff --git a/test/deprecated/quantization/test_quant_aware.py b/test/deprecated/quantization/test_quant_aware_deprecated.py similarity index 100% rename from test/deprecated/quantization/test_quant_aware.py rename to test/deprecated/quantization/test_quant_aware_deprecated.py diff --git a/test/deprecated/quantization/test_quant_aware_user_defined.py b/test/deprecated/quantization/test_quant_aware_user_defined.py index 3521ecf7ddeff6..5c7e48a854e68d 100644 --- a/test/deprecated/quantization/test_quant_aware_user_defined.py +++ b/test/deprecated/quantization/test_quant_aware_user_defined.py @@ -14,9 +14,12 @@ import logging import os import unittest +from test.deprecated.quantization.test_quant_aware_deprecated import ( + MobileNet, + StaticCase, +) import numpy as np -from test_quant_aware import MobileNet, StaticCase import paddle from paddle.static.quantization.quanter import convert, quant_aware diff --git a/test/deprecated/quantization/test_quant_post_quant_aware.py b/test/deprecated/quantization/test_quant_post_quant_aware.py index 0fe582306fbd72..d02c523f5b7c9f 100644 --- a/test/deprecated/quantization/test_quant_post_quant_aware.py +++ b/test/deprecated/quantization/test_quant_post_quant_aware.py @@ -15,9 +15,9 @@ import logging import random import unittest +from test.deprecated.quantization.test_quant_aware_deprecated import StaticCase import numpy as np -from test_quant_aware import StaticCase import paddle from paddle.static.quantization.quanter import convert, quant_aware diff --git a/test/deprecated/quantization/test_quantization_mkldnn_pass.py b/test/deprecated/quantization/test_quantization_mkldnn_pass_deprecated.py similarity index 100% rename from test/deprecated/quantization/test_quantization_mkldnn_pass.py rename to test/deprecated/quantization/test_quantization_mkldnn_pass_deprecated.py diff --git a/test/deprecated/quantization/test_quantization_pass.py b/test/deprecated/quantization/test_quantization_pass_deprecated.py similarity index 100% rename from test/deprecated/quantization/test_quantization_pass.py rename to test/deprecated/quantization/test_quantization_pass_deprecated.py diff --git a/test/deprecated/quantization/test_quantization_scale_pass.py b/test/deprecated/quantization/test_quantization_scale_pass_deprecated.py similarity index 100% rename from test/deprecated/quantization/test_quantization_scale_pass.py rename to test/deprecated/quantization/test_quantization_scale_pass_deprecated.py diff --git a/test/deprecated/quantization/test_user_defined_quantization.py b/test/deprecated/quantization/test_user_defined_quantization_deprecated.py similarity index 100% rename from test/deprecated/quantization/test_user_defined_quantization.py rename to test/deprecated/quantization/test_user_defined_quantization_deprecated.py diff --git a/test/deprecated/rnn/CMakeLists.txt b/test/deprecated/rnn/CMakeLists.txt index 04773499b35917..a06731560086dd 100644 --- a/test/deprecated/rnn/CMakeLists.txt +++ b/test/deprecated/rnn/CMakeLists.txt @@ -9,5 +9,7 @@ foreach(TEST_OP ${TEST_OPS}) endforeach() if(NOT WIN32) set_tests_properties(test_rnn_nets_static PROPERTIES TIMEOUT 120) + set_tests_properties(test_rnn_nets_static_deprecated PROPERTIES TIMEOUT 120) set_tests_properties(test_rnn_nets PROPERTIES TIMEOUT 120) + set_tests_properties(test_rnn_nets_deprecated PROPERTIES TIMEOUT 120) endif() diff --git a/test/deprecated/rnn/test_rnn_nets.py b/test/deprecated/rnn/test_rnn_nets.py index f87424245ce811..6cec726472c58c 100644 --- a/test/deprecated/rnn/test_rnn_nets.py +++ b/test/deprecated/rnn/test_rnn_nets.py @@ -95,37 +95,12 @@ def test_with_zero_state(self): np.testing.assert_allclose(y1, y2.numpy(), atol=1e-8, rtol=1e-5) np.testing.assert_allclose(h1, h2.numpy(), atol=1e-8, rtol=1e-5) - def test_with_input_lengths(self): - rnn1 = self.rnn1 - rnn2 = self.rnn2 - - x = np.random.randn(12, 4, 16) - if not self.time_major: - x = np.transpose(x, [1, 0, 2]) - sequence_length = np.array([12, 10, 9, 8], dtype=np.int64) - - y1, h1 = rnn1(x, sequence_length=sequence_length) - - seq_len = paddle.to_tensor(sequence_length) - mask = paddle.static.nn.sequence_lod.sequence_mask( - seq_len, dtype=paddle.get_default_dtype() - ) - if self.time_major: - mask = paddle.transpose(mask, [1, 0]) - y2, h2 = rnn2(paddle.to_tensor(x), sequence_length=seq_len) - mask = paddle.unsqueeze(mask, -1) - y2 = paddle.multiply(y2, mask) - - np.testing.assert_allclose(y1, y2.numpy(), atol=1e-8, rtol=1e-5) - np.testing.assert_allclose(h1, h2.numpy(), atol=1e-8, rtol=1e-5) - def test_predict(self): predict_test_util(self.place, "SimpleRNN") def runTest(self): self.test_with_initial_state() self.test_with_zero_state() - self.test_with_input_lengths() self.test_predict() @@ -180,37 +155,12 @@ def test_with_zero_state(self): np.testing.assert_allclose(y1, y2.numpy(), atol=1e-8, rtol=1e-5) np.testing.assert_allclose(h1, h2.numpy(), atol=1e-8, rtol=1e-5) - def test_with_input_lengths(self): - rnn1 = self.rnn1 - rnn2 = self.rnn2 - - x = np.random.randn(12, 4, 16) - if not self.time_major: - x = np.transpose(x, [1, 0, 2]) - sequence_length = np.array([12, 10, 9, 8], dtype=np.int64) - - y1, h1 = rnn1(x, sequence_length=sequence_length) - - seq_len = paddle.to_tensor(sequence_length) - mask = paddle.static.nn.sequence_lod.sequence_mask( - seq_len, dtype=paddle.get_default_dtype() - ) - if self.time_major: - mask = paddle.transpose(mask, [1, 0]) - y2, h2 = rnn2(paddle.to_tensor(x), sequence_length=seq_len) - mask = paddle.unsqueeze(mask, -1) - y2 = paddle.multiply(y2, mask) - - np.testing.assert_allclose(y1, y2.numpy(), atol=1e-8, rtol=1e-5) - np.testing.assert_allclose(h1, h2.numpy(), atol=1e-8, rtol=1e-5) - def test_predict(self): predict_test_util(self.place, "GRU") def runTest(self): self.test_with_initial_state() self.test_with_zero_state() - self.test_with_input_lengths() self.test_predict() @@ -273,31 +223,6 @@ def test_with_zero_state(self): np.testing.assert_allclose(h1, h2.numpy(), atol=1e-8, rtol=1e-5) np.testing.assert_allclose(c1, c2.numpy(), atol=1e-8, rtol=1e-5) - def test_with_input_lengths(self): - rnn1 = self.rnn1 - rnn2 = self.rnn2 - - x = np.random.randn(12, 4, 16) - if not self.time_major: - x = np.transpose(x, [1, 0, 2]) - sequence_length = np.array([12, 10, 9, 8], dtype=np.int64) - - y1, (h1, c1) = rnn1(x, sequence_length=sequence_length) - - seq_len = paddle.to_tensor(sequence_length) - mask = paddle.static.nn.sequence_lod.sequence_mask( - seq_len, dtype=paddle.get_default_dtype() - ) - if self.time_major: - mask = paddle.transpose(mask, [1, 0]) - y2, (h2, c2) = rnn2(paddle.to_tensor(x), sequence_length=seq_len) - mask = paddle.unsqueeze(mask, -1) - y2 = paddle.multiply(y2, mask) - - np.testing.assert_allclose(y1, y2.numpy(), atol=1e-8, rtol=1e-5) - np.testing.assert_allclose(h1, h2.numpy(), atol=1e-8, rtol=1e-5) - np.testing.assert_allclose(c1, c2.numpy(), atol=1e-8, rtol=1e-5) - def test_predict(self): predict_test_util(self.place, "LSTM") predict_test_util(self.place, "LSTM", False) @@ -305,7 +230,6 @@ def test_predict(self): def runTest(self): self.test_with_initial_state() self.test_with_zero_state() - self.test_with_input_lengths() self.test_predict() diff --git a/test/deprecated/rnn/test_rnn_nets_deprecated.py b/test/deprecated/rnn/test_rnn_nets_deprecated.py new file mode 100644 index 00000000000000..ee435a1235ef78 --- /dev/null +++ b/test/deprecated/rnn/test_rnn_nets_deprecated.py @@ -0,0 +1,327 @@ +# Copyright (c) 2020 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 paddle + +paddle.set_default_dtype("float64") +import os +import sys +import tempfile +import unittest + +import numpy as np +from convert import convert_params_for_net + +from paddle.pir_utils import test_with_dygraph_pir + +sys.path.append("../../rnn") +from rnn_numpy import GRU, LSTM, SimpleRNN + +bidirectional_list = ["bidirectional", "bidirect"] + + +class TestSimpleRNN(unittest.TestCase): + def __init__( + self, time_major=True, direction="forward", place="cpu", mode='RNN_TANH' + ): + super().__init__("runTest") + self.time_major = time_major + self.direction = direction + self.num_directions = 2 if direction in bidirectional_list else 1 + self.place = place + self.mode = mode + + def setUp(self): + # Since `set_device` is global, set `set_device` in `setUp` rather than + # `__init__` to avoid using an error device set by another test case. + place = paddle.set_device(self.place) + paddle.disable_static(place) + rnn1 = SimpleRNN( + 16, + 32, + 2, + time_major=self.time_major, + direction=self.direction, + nonlinearity=self.mode, + ) + rnn2 = paddle.nn.SimpleRNN( + 16, + 32, + 2, + time_major=self.time_major, + direction=self.direction, + activation=self.mode[4:].lower(), + ) + convert_params_for_net(rnn1, rnn2) + + self.rnn1 = rnn1 + self.rnn2 = rnn2 + + def test_with_input_lengths(self): + rnn1 = self.rnn1 + rnn2 = self.rnn2 + + x = np.random.randn(12, 4, 16) + if not self.time_major: + x = np.transpose(x, [1, 0, 2]) + sequence_length = np.array([12, 10, 9, 8], dtype=np.int64) + + y1, h1 = rnn1(x, sequence_length=sequence_length) + + seq_len = paddle.to_tensor(sequence_length) + mask = paddle.static.nn.sequence_lod.sequence_mask( + seq_len, dtype=paddle.get_default_dtype() + ) + if self.time_major: + mask = paddle.transpose(mask, [1, 0]) + y2, h2 = rnn2(paddle.to_tensor(x), sequence_length=seq_len) + mask = paddle.unsqueeze(mask, -1) + y2 = paddle.multiply(y2, mask) + + np.testing.assert_allclose(y1, y2.numpy(), atol=1e-8, rtol=1e-5) + np.testing.assert_allclose(h1, h2.numpy(), atol=1e-8, rtol=1e-5) + + def runTest(self): + self.test_with_input_lengths() + + +class TestGRU(unittest.TestCase): + def __init__(self, time_major=True, direction="forward", place="cpu"): + super().__init__("runTest") + self.time_major = time_major + self.direction = direction + self.num_directions = 2 if direction in bidirectional_list else 1 + self.place = place + + def setUp(self): + # Since `set_device` is global, set `set_device` in `setUp` rather than + # `__init__` to avoid using an error device set by another test case. + place = paddle.set_device(self.place) + paddle.disable_static(place) + rnn1 = GRU( + 16, 32, 2, time_major=self.time_major, direction=self.direction + ) + rnn2 = paddle.nn.GRU( + 16, 32, 2, time_major=self.time_major, direction=self.direction + ) + convert_params_for_net(rnn1, rnn2) + + self.rnn1 = rnn1 + self.rnn2 = rnn2 + + def test_with_input_lengths(self): + rnn1 = self.rnn1 + rnn2 = self.rnn2 + + x = np.random.randn(12, 4, 16) + if not self.time_major: + x = np.transpose(x, [1, 0, 2]) + sequence_length = np.array([12, 10, 9, 8], dtype=np.int64) + + y1, h1 = rnn1(x, sequence_length=sequence_length) + + seq_len = paddle.to_tensor(sequence_length) + mask = paddle.static.nn.sequence_lod.sequence_mask( + seq_len, dtype=paddle.get_default_dtype() + ) + if self.time_major: + mask = paddle.transpose(mask, [1, 0]) + y2, h2 = rnn2(paddle.to_tensor(x), sequence_length=seq_len) + mask = paddle.unsqueeze(mask, -1) + y2 = paddle.multiply(y2, mask) + + np.testing.assert_allclose(y1, y2.numpy(), atol=1e-8, rtol=1e-5) + np.testing.assert_allclose(h1, h2.numpy(), atol=1e-8, rtol=1e-5) + + def runTest(self): + self.test_with_input_lengths() + + +class TestLSTM(unittest.TestCase): + def __init__(self, time_major=True, direction="forward", place="cpu"): + super().__init__("runTest") + self.time_major = time_major + self.direction = direction + self.num_directions = 2 if direction in bidirectional_list else 1 + self.place = place + + def setUp(self): + # Since `set_device` is global, set `set_device` in `setUp` rather than + # `__init__` to avoid using an error device set by another test case. + place = paddle.set_device(self.place) + paddle.disable_static(place) + rnn1 = LSTM( + 16, 32, 2, time_major=self.time_major, direction=self.direction + ) + rnn2 = paddle.nn.LSTM( + 16, 32, 2, time_major=self.time_major, direction=self.direction + ) + convert_params_for_net(rnn1, rnn2) + + self.rnn1 = rnn1 + self.rnn2 = rnn2 + + def test_with_input_lengths(self): + rnn1 = self.rnn1 + rnn2 = self.rnn2 + + x = np.random.randn(12, 4, 16) + if not self.time_major: + x = np.transpose(x, [1, 0, 2]) + sequence_length = np.array([12, 10, 9, 8], dtype=np.int64) + + y1, (h1, c1) = rnn1(x, sequence_length=sequence_length) + + seq_len = paddle.to_tensor(sequence_length) + mask = paddle.static.nn.sequence_lod.sequence_mask( + seq_len, dtype=paddle.get_default_dtype() + ) + if self.time_major: + mask = paddle.transpose(mask, [1, 0]) + y2, (h2, c2) = rnn2(paddle.to_tensor(x), sequence_length=seq_len) + mask = paddle.unsqueeze(mask, -1) + y2 = paddle.multiply(y2, mask) + + np.testing.assert_allclose(y1, y2.numpy(), atol=1e-8, rtol=1e-5) + np.testing.assert_allclose(h1, h2.numpy(), atol=1e-8, rtol=1e-5) + np.testing.assert_allclose(c1, c2.numpy(), atol=1e-8, rtol=1e-5) + + def runTest(self): + self.test_with_input_lengths() + + +class TestLSTMWithProjSize(TestLSTM): + def setUp(self): + # Since `set_device` is global, set `set_device` in `setUp` rather than + # `__init__` to avoid using an error device set by another test case. + place = paddle.set_device(self.place) + paddle.disable_static(place) + rnn1 = LSTM( + 16, + 32, + 2, + time_major=self.time_major, + direction=self.direction, + proj_size=8, + ) + rnn2 = paddle.nn.LSTM( + 16, + 32, + 2, + time_major=self.time_major, + direction=self.direction, + proj_size=8, + ) + convert_params_for_net(rnn1, rnn2) + + self.rnn1 = rnn1 + self.rnn2 = rnn2 + self.proj_size = 8 + + +@test_with_dygraph_pir +def predict_test_util(place, mode, stop_gradient=True): + place = paddle.set_device(place) + paddle.seed(123) + np.random.seed(123) + + class Net(paddle.nn.Layer): + def __init__(self): + super().__init__() + self.rnn = getattr(paddle.nn, mode)( + 16, 32, 2, direction="bidirectional", dropout=0.1 + ) + + def forward(self, input): + return self.rnn(input) + + x = paddle.randn((4, 10, 16)) + x.stop_gradient = stop_gradient + seq_len = paddle.to_tensor(np.array([10, 6, 8, 5])) + mask = paddle.static.nn.sequence_lod.sequence_mask( + seq_len, maxlen=10, dtype=x.dtype + ) + mask = paddle.unsqueeze(mask, [2]) + rnn = Net() + y, _ = rnn(x) + y = y * mask + loss = paddle.mean(y) + loss.backward() + optimizer = paddle.optimizer.Adam( + learning_rate=0.1, parameters=rnn.parameters() + ) + optimizer.step() + rnn.eval() + y, _ = rnn(x) + # `jit.to_static` would include a train_program, eval mode might cause + # some errors currently, such as dropout grad op gets `is_test == True`. + rnn.train() + + rnn = paddle.jit.to_static( + rnn, + [paddle.static.InputSpec(shape=[None, None, 16], dtype=x.dtype)], + full_graph=True, + ) + temp_dir = tempfile.TemporaryDirectory() + save_dirname = os.path.join(temp_dir.name, "./inference/%s_infer" % mode) + + paddle.jit.save(rnn, save_dirname) + + paddle.enable_static() + + new_scope = paddle.static.Scope() + with paddle.static.scope_guard(new_scope): + exe = paddle.static.Executor(place) + [ + inference_program, + feed_target_names, + fetch_targets, + ] = paddle.static.load_inference_model(save_dirname, exe) + results = exe.run( + inference_program, + feed={feed_target_names[0]: x.numpy()}, + fetch_list=fetch_targets, + ) + np.testing.assert_equal( + y.numpy(), results[0] + ) # eval results equal predict results + paddle.disable_static() + + temp_dir.cleanup() + + +def load_tests(loader, tests, pattern): + suite = unittest.TestSuite() + 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: + for test_class in [ + TestSimpleRNN, + TestLSTM, + TestGRU, + TestLSTMWithProjSize, + ]: + suite.addTest(test_class(time_major, direction, device)) + if test_class == TestSimpleRNN: + suite.addTest( + test_class( + time_major, direction, device, mode="RNN_RELU" + ) + ) + return suite + + +if __name__ == '__main__': + unittest.main() diff --git a/test/deprecated/rnn/test_rnn_nets_static.py b/test/deprecated/rnn/test_rnn_nets_static.py index da00c37682fae5..3ccdad1dfc71e7 100644 --- a/test/deprecated/rnn/test_rnn_nets_static.py +++ b/test/deprecated/rnn/test_rnn_nets_static.py @@ -150,50 +150,9 @@ def test_with_zero_state(self): np.testing.assert_allclose(y1, y2, atol=1e-8, rtol=1e-5) np.testing.assert_allclose(h1, h2, atol=1e-8, rtol=1e-5) - def test_with_input_lengths(self): - mp = self.mp.clone() - sp = self.sp - rnn1 = self.rnn1 - rnn2 = self.rnn2 - exe = self.executor - scope = self.scope - - x = np.random.randn(12, 4, 16) - if not self.time_major: - x = np.transpose(x, [1, 0, 2]) - sequence_length = np.array([12, 10, 9, 8], dtype=np.int64) - - y1, h1 = rnn1(x, sequence_length=sequence_length) - - with paddle.base.unique_name.guard(): - with paddle.static.program_guard(mp, sp): - x_data = paddle.static.data( - "input", - [-1, -1, 16], - dtype=paddle.framework.get_default_dtype(), - ) - seq_len = paddle.static.data("seq_len", [-1], dtype="int64") - mask = paddle.static.nn.sequence_lod.sequence_mask( - seq_len, dtype=paddle.get_default_dtype() - ) - if self.time_major: - mask = paddle.transpose(mask, [1, 0]) - y, h = rnn2(x_data, sequence_length=seq_len) - mask = paddle.unsqueeze(mask, -1) - y = paddle.multiply(y, mask) - - feed_dict = {x_data.name: x, seq_len.name: sequence_length} - - with paddle.static.scope_guard(scope): - y2, h2 = exe.run(mp, feed=feed_dict, fetch_list=[y, h]) - - np.testing.assert_allclose(y1, y2, atol=1e-8, rtol=1e-5) - np.testing.assert_allclose(h1, h2, atol=1e-8, rtol=1e-5) - def runTest(self): self.test_with_initial_state() self.test_with_zero_state() - self.test_with_input_lengths() class TestGRU(unittest.TestCase): @@ -307,46 +266,6 @@ def test_with_zero_state(self): np.testing.assert_allclose(y1, y2, atol=1e-8, rtol=1e-5) np.testing.assert_allclose(h1, h2, atol=1e-8, rtol=1e-5) - def test_with_input_lengths(self): - mp = self.mp.clone() - sp = self.sp - rnn1 = self.rnn1 - rnn2 = self.rnn2 - exe = self.executor - scope = self.scope - - x = np.random.randn(12, 4, 16) - if not self.time_major: - x = np.transpose(x, [1, 0, 2]) - sequence_length = np.array([12, 10, 9, 8], dtype=np.int64) - - y1, h1 = rnn1(x, sequence_length=sequence_length) - - with paddle.base.unique_name.guard(): - with paddle.static.program_guard(mp, sp): - x_data = paddle.static.data( - "input", - [-1, -1, 16], - dtype=paddle.framework.get_default_dtype(), - ) - seq_len = paddle.static.data("seq_len", [-1], dtype="int64") - mask = paddle.static.nn.sequence_lod.sequence_mask( - seq_len, dtype=paddle.get_default_dtype() - ) - if self.time_major: - mask = paddle.transpose(mask, [1, 0]) - y, h = rnn2(x_data, sequence_length=seq_len) - mask = paddle.unsqueeze(mask, -1) - y = paddle.multiply(y, mask) - - feed_dict = {x_data.name: x, seq_len.name: sequence_length} - - with paddle.static.scope_guard(scope): - y2, h2 = exe.run(mp, feed=feed_dict, fetch_list=[y, h]) - - np.testing.assert_allclose(y1, y2, atol=1e-8, rtol=1e-5) - np.testing.assert_allclose(h1, h2, atol=1e-8, rtol=1e-5) - def runTest(self): self.test_with_initial_state() self.test_with_zero_state() @@ -476,51 +395,9 @@ def test_with_zero_state(self): np.testing.assert_allclose(h1, h2, atol=1e-8, rtol=1e-5) np.testing.assert_allclose(c1, c2, atol=1e-8, rtol=1e-5) - def test_with_input_lengths(self): - mp = self.mp.clone() - sp = self.sp - rnn1 = self.rnn1 - rnn2 = self.rnn2 - exe = self.executor - scope = self.scope - - x = np.random.randn(12, 4, 16) - if not self.time_major: - x = np.transpose(x, [1, 0, 2]) - sequence_length = np.array([12, 10, 9, 8], dtype=np.int64) - - y1, (h1, c1) = rnn1(x, sequence_length=sequence_length) - - with paddle.base.unique_name.guard(): - with paddle.static.program_guard(mp, sp): - x_data = paddle.static.data( - "input", - [-1, -1, 16], - dtype=paddle.framework.get_default_dtype(), - ) - seq_len = paddle.static.data("seq_len", [-1], dtype="int64") - mask = paddle.static.nn.sequence_lod.sequence_mask( - seq_len, dtype=paddle.get_default_dtype() - ) - if self.time_major: - mask = paddle.transpose(mask, [1, 0]) - y, (h, c) = rnn2(x_data, sequence_length=seq_len) - mask = paddle.unsqueeze(mask, -1) - y = paddle.multiply(y, mask) - - feed_dict = {x_data.name: x, seq_len.name: sequence_length} - - with paddle.static.scope_guard(scope): - y2, h2, c2 = exe.run(mp, feed=feed_dict, fetch_list=[y, h, c]) - - np.testing.assert_allclose(y1, y2, atol=1e-8, rtol=1e-5) - np.testing.assert_allclose(h1, h2, atol=1e-8, rtol=1e-5) - np.testing.assert_allclose(c1, c2, atol=1e-8, rtol=1e-5) - def runTest(self): self.test_with_initial_state() self.test_with_zero_state() - self.test_with_input_lengths() class TestLSTMWithProjSize(TestLSTM): diff --git a/test/deprecated/rnn/test_rnn_nets_static_deprecated.py b/test/deprecated/rnn/test_rnn_nets_static_deprecated.py new file mode 100644 index 00000000000000..8e9554ecb93f4f --- /dev/null +++ b/test/deprecated/rnn/test_rnn_nets_static_deprecated.py @@ -0,0 +1,369 @@ +# Copyright (c) 2020 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 paddle + +paddle.set_default_dtype("float64") + + +paddle.enable_static() + +import sys +import unittest + +import numpy as np +from convert import convert_params_for_net_static + +sys.path.append("../../rnn") +from rnn_numpy import GRU, LSTM, SimpleRNN + +bidirectional_list = ["bidirectional", "bidirect"] + + +class TestSimpleRNN(unittest.TestCase): + def __init__( + self, time_major=True, direction="forward", place="cpu", mode="RNN_TANH" + ): + super().__init__("runTest") + self.time_major = time_major + self.direction = direction + self.num_directions = 2 if direction in bidirectional_list else 1 + self.place = place + self.mode = mode + + def setUp(self): + # Since `set_device` is global, set `set_device` in `setUp` rather than + # `__init__` to avoid using an error device set by another test case. + place = paddle.set_device(self.place) + rnn1 = SimpleRNN( + 16, + 32, + 2, + time_major=self.time_major, + direction=self.direction, + nonlinearity=self.mode, + ) + + mp = paddle.static.Program() + sp = paddle.static.Program() + with paddle.base.unique_name.guard(): + with paddle.static.program_guard(mp, sp): + rnn2 = paddle.nn.SimpleRNN( + 16, + 32, + 2, + time_major=self.time_major, + direction=self.direction, + activation=self.mode[4:].lower(), + ) + + exe = paddle.static.Executor(place) + scope = paddle.base.Scope() + with paddle.static.scope_guard(scope): + exe.run(sp) + convert_params_for_net_static(rnn1, rnn2, place) + + self.mp = mp + self.sp = sp + self.rnn1 = rnn1 + self.rnn2 = rnn2 + + self.place = place + self.executor = exe + self.scope = scope + + def test_with_input_lengths(self): + mp = self.mp.clone() + sp = self.sp + rnn1 = self.rnn1 + rnn2 = self.rnn2 + exe = self.executor + scope = self.scope + + x = np.random.randn(12, 4, 16) + if not self.time_major: + x = np.transpose(x, [1, 0, 2]) + sequence_length = np.array([12, 10, 9, 8], dtype=np.int64) + + y1, h1 = rnn1(x, sequence_length=sequence_length) + + with paddle.base.unique_name.guard(): + with paddle.static.program_guard(mp, sp): + x_data = paddle.static.data( + "input", + [-1, -1, 16], + dtype=paddle.framework.get_default_dtype(), + ) + seq_len = paddle.static.data("seq_len", [-1], dtype="int64") + mask = paddle.static.nn.sequence_lod.sequence_mask( + seq_len, dtype=paddle.get_default_dtype() + ) + if self.time_major: + mask = paddle.transpose(mask, [1, 0]) + y, h = rnn2(x_data, sequence_length=seq_len) + mask = paddle.unsqueeze(mask, -1) + y = paddle.multiply(y, mask) + + feed_dict = {x_data.name: x, seq_len.name: sequence_length} + + with paddle.static.scope_guard(scope): + y2, h2 = exe.run(mp, feed=feed_dict, fetch_list=[y, h]) + + np.testing.assert_allclose(y1, y2, atol=1e-8, rtol=1e-5) + np.testing.assert_allclose(h1, h2, atol=1e-8, rtol=1e-5) + + def runTest(self): + self.test_with_input_lengths() + + +class TestGRU(unittest.TestCase): + def __init__(self, time_major=True, direction="forward", place="cpu"): + super().__init__("runTest") + self.time_major = time_major + self.direction = direction + self.num_directions = 2 if direction in bidirectional_list else 1 + self.place = place + + def setUp(self): + # Since `set_device` is global, set `set_device` in `setUp` rather than + # `__init__` to avoid using an error device set by another test case. + place = paddle.set_device(self.place) + rnn1 = GRU( + 16, 32, 2, time_major=self.time_major, direction=self.direction + ) + + mp = paddle.static.Program() + sp = paddle.static.Program() + with paddle.base.unique_name.guard(): + with paddle.static.program_guard(mp, sp): + rnn2 = paddle.nn.GRU( + 16, + 32, + 2, + time_major=self.time_major, + direction=self.direction, + ) + + exe = paddle.static.Executor(place) + scope = paddle.base.Scope() + with paddle.static.scope_guard(scope): + exe.run(sp) + convert_params_for_net_static(rnn1, rnn2, place) + + self.mp = mp + self.sp = sp + self.rnn1 = rnn1 + self.rnn2 = rnn2 + + self.place = place + self.executor = exe + self.scope = scope + + def test_with_input_lengths(self): + mp = self.mp.clone() + sp = self.sp + rnn1 = self.rnn1 + rnn2 = self.rnn2 + exe = self.executor + scope = self.scope + + x = np.random.randn(12, 4, 16) + if not self.time_major: + x = np.transpose(x, [1, 0, 2]) + sequence_length = np.array([12, 10, 9, 8], dtype=np.int64) + + y1, h1 = rnn1(x, sequence_length=sequence_length) + + with paddle.base.unique_name.guard(): + with paddle.static.program_guard(mp, sp): + x_data = paddle.static.data( + "input", + [-1, -1, 16], + dtype=paddle.framework.get_default_dtype(), + ) + seq_len = paddle.static.data("seq_len", [-1], dtype="int64") + mask = paddle.static.nn.sequence_lod.sequence_mask( + seq_len, dtype=paddle.get_default_dtype() + ) + if self.time_major: + mask = paddle.transpose(mask, [1, 0]) + y, h = rnn2(x_data, sequence_length=seq_len) + mask = paddle.unsqueeze(mask, -1) + y = paddle.multiply(y, mask) + + feed_dict = {x_data.name: x, seq_len.name: sequence_length} + + with paddle.static.scope_guard(scope): + y2, h2 = exe.run(mp, feed=feed_dict, fetch_list=[y, h]) + + np.testing.assert_allclose(y1, y2, atol=1e-8, rtol=1e-5) + np.testing.assert_allclose(h1, h2, atol=1e-8, rtol=1e-5) + + +class TestLSTM(unittest.TestCase): + def __init__(self, time_major=True, direction="forward", place="cpu"): + super().__init__("runTest") + self.time_major = time_major + self.direction = direction + self.num_directions = 2 if direction in bidirectional_list else 1 + self.place = place + + def setUp(self): + # Since `set_device` is global, set `set_device` in `setUp` rather than + # `__init__` to avoid using an error device set by another test case. + place = paddle.set_device(self.place) + rnn1 = LSTM( + 16, 32, 2, time_major=self.time_major, direction=self.direction + ) + + mp = paddle.static.Program() + sp = paddle.static.Program() + with paddle.base.unique_name.guard(): + with paddle.static.program_guard(mp, sp): + rnn2 = paddle.nn.LSTM( + 16, + 32, + 2, + time_major=self.time_major, + direction=self.direction, + ) + + exe = paddle.static.Executor(place) + scope = paddle.base.Scope() + with paddle.static.scope_guard(scope): + exe.run(sp) + convert_params_for_net_static(rnn1, rnn2, place) + + self.mp = mp + self.sp = sp + self.rnn1 = rnn1 + self.rnn2 = rnn2 + + self.place = place + self.executor = exe + self.scope = scope + + def test_with_input_lengths(self): + mp = self.mp.clone() + sp = self.sp + rnn1 = self.rnn1 + rnn2 = self.rnn2 + exe = self.executor + scope = self.scope + + x = np.random.randn(12, 4, 16) + if not self.time_major: + x = np.transpose(x, [1, 0, 2]) + sequence_length = np.array([12, 10, 9, 8], dtype=np.int64) + + y1, (h1, c1) = rnn1(x, sequence_length=sequence_length) + + with paddle.base.unique_name.guard(): + with paddle.static.program_guard(mp, sp): + x_data = paddle.static.data( + "input", + [-1, -1, 16], + dtype=paddle.framework.get_default_dtype(), + ) + seq_len = paddle.static.data("seq_len", [-1], dtype="int64") + mask = paddle.static.nn.sequence_lod.sequence_mask( + seq_len, dtype=paddle.get_default_dtype() + ) + if self.time_major: + mask = paddle.transpose(mask, [1, 0]) + y, (h, c) = rnn2(x_data, sequence_length=seq_len) + mask = paddle.unsqueeze(mask, -1) + y = paddle.multiply(y, mask) + + feed_dict = {x_data.name: x, seq_len.name: sequence_length} + + with paddle.static.scope_guard(scope): + y2, h2, c2 = exe.run(mp, feed=feed_dict, fetch_list=[y, h, c]) + + np.testing.assert_allclose(y1, y2, atol=1e-8, rtol=1e-5) + np.testing.assert_allclose(h1, h2, atol=1e-8, rtol=1e-5) + np.testing.assert_allclose(c1, c2, atol=1e-8, rtol=1e-5) + + def runTest(self): + self.test_with_input_lengths() + + +class TestLSTMWithProjSize(TestLSTM): + def setUp(self): + # Since `set_device` is global, set `set_device` in `setUp` rather than + # `__init__` to avoid using an error device set by another test case. + place = paddle.set_device(self.place) + rnn1 = LSTM( + 16, + 32, + 2, + time_major=self.time_major, + direction=self.direction, + proj_size=8, + ) + + mp = paddle.static.Program() + sp = paddle.static.Program() + with paddle.base.unique_name.guard(): + with paddle.static.program_guard(mp, sp): + rnn2 = paddle.nn.LSTM( + 16, + 32, + 2, + time_major=self.time_major, + direction=self.direction, + proj_size=8, + ) + + exe = paddle.static.Executor(place) + scope = paddle.base.Scope() + with paddle.static.scope_guard(scope): + exe.run(sp) + convert_params_for_net_static(rnn1, rnn2, place) + + self.mp = mp + self.sp = sp + self.rnn1 = rnn1 + self.rnn2 = rnn2 + self.proj_size = 8 + + self.place = place + self.executor = exe + self.scope = scope + + +def load_tests(loader, tests, pattern): + suite = unittest.TestSuite() + 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: + for test_class in [ + TestSimpleRNN, + TestLSTM, + TestGRU, + TestLSTMWithProjSize, + ]: + suite.addTest(test_class(time_major, direction, device)) + if test_class == TestSimpleRNN: + suite.addTest( + test_class( + time_major, direction, device, mode="RNN_RELU" + ) + ) + return suite + + +if __name__ == "__main__": + unittest.main() diff --git a/test/deprecated/standalone_executor/test_standalone_dist_attr_run_time_set_get.py b/test/deprecated/standalone_executor/test_standalone_dist_attr_run_time_set_get_deprecated.py similarity index 100% rename from test/deprecated/standalone_executor/test_standalone_dist_attr_run_time_set_get.py rename to test/deprecated/standalone_executor/test_standalone_dist_attr_run_time_set_get_deprecated.py diff --git a/test/deprecated/standalone_executor/test_standalone_executor.py b/test/deprecated/standalone_executor/test_standalone_executor.py index 934558c170f513..c4b86d4284454f 100644 --- a/test/deprecated/standalone_executor/test_standalone_executor.py +++ b/test/deprecated/standalone_executor/test_standalone_executor.py @@ -20,7 +20,6 @@ import unittest import numpy as np -from utils import static_guard import paddle from paddle.base import core @@ -137,120 +136,6 @@ def run_test(self, use_new_executor=True): return outs -class SwitchExecutorInterfaceWithFeed(unittest.TestCase): - def setUp(self): - self.place = ( - paddle.CUDAPlace(0) - if core.is_compiled_with_cuda() - else paddle.CPUPlace() - ) - self.iter_run = 2 - - def build_program(self, is_double=False): - main_program = paddle.static.Program() - startup_program = paddle.static.Program() - with paddle.static.program_guard(main_program, startup_program): - a = paddle.static.data(name="a", shape=[2, 2], dtype='float32') - b = paddle.ones([2, 2]) * 2 - t = paddle.static.nn.fc(a, 2) - c = t + b - if is_double: - c = c + c - - return main_program, startup_program, [c] - - def _run( - self, - feed, - use_str=False, - is_double=False, - add_wrong_fetch=False, - use_compiled=False, - ): - paddle.seed(2020) - - main_program, startup_program, fetch_vars = self.build_program( - is_double - ) - - exe = paddle.static.Executor(self.place) - exe.run(startup_program) - - if use_compiled: - main_program = paddle.static.CompiledProgram(main_program) - - if use_str: # test for fetch name - fetch_vars = [x.name for x in fetch_vars] - if add_wrong_fetch: # test for wrong fetch type - fetch_vars.append(1123) - outs = [] - for i in range(self.iter_run): - out = exe.run(main_program, feed=feed, fetch_list=fetch_vars)[0] - - outs.append(out) - - return outs - - def run_dygraph(self, feed): - def run_once(is_double): - paddle.seed(2020) - a = feed['a'] - a = paddle.to_tensor(a, dtype='float32') - b = paddle.ones([2, 2]) * 2 - t = paddle.nn.Linear(2, 2)(a) - c = t + b - if is_double: - c = c + c - return c.numpy() - - out1 = [] - for i in range(self.iter_run): - out1.append(run_once(False)) - out2 = [] - for i in range(self.iter_run): - out2.append(run_once(True)) - return [out1, out2] - - def run_new_executor(self, feed, use_compiled=False): - # run construct program 1 - out1 = self._run( - feed, use_str=False, is_double=False, use_compiled=use_compiled - ) - # run construct program 2 with same executor - out2 = self._run( - feed, use_str=True, is_double=True, use_compiled=use_compiled - ) - - return [out1, out2] - - def test_with_feed(self): - data = np.ones([2, 2], dtype="float32") - feed = {"a": data, 'fake_input': data} - - with static_guard(): - res = self.run_new_executor(feed) - with paddle.base.dygraph.guard(): - gt = self.run_dygraph(feed) - for x, y in zip(gt, res): - np.testing.assert_array_equal(x, y) - - def test_with_error(self): - feed = [{'a': np.ones([2, 2], dtype="float32")}] - - with self.assertRaises(TypeError): - self._run(feed[0], add_wrong_fetch=True) - - def test_empty_program(self): - program = paddle.static.Program() - exe = paddle.static.Executor(self.place) - for i in range(10): - out = exe.run() # old executor - - for i in range(10): - print(i, flush=1) - out = exe.run(program, feed=None) - - class TestException(unittest.TestCase): def setUp(self): self.place = paddle.CPUPlace() diff --git a/test/deprecated/standalone_executor/test_standalone_executor_deprecated.py b/test/deprecated/standalone_executor/test_standalone_executor_deprecated.py new file mode 100644 index 00000000000000..930cc4c28907cb --- /dev/null +++ b/test/deprecated/standalone_executor/test_standalone_executor_deprecated.py @@ -0,0 +1,175 @@ +# 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 os + +os.environ['FLAGS_use_stream_safe_cuda_allocator'] = "true" +import unittest + +import numpy as np +from utils import static_guard + +import paddle +from paddle.base import core + +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): + with paddle.static.device_guard('cpu'): + data = paddle.ones([4, 64], dtype='float32', name='data') + + # data -> [memcpy_h2d] -> data' -> [matmul] -> out ->[add] -> add_out + with paddle.static.device_guard('gpu'): + weight = paddle.randn([64, 64], name='weight') # gpu + matmul_out = paddle.matmul(data, weight, name='matmul_out') # gpus + bias = paddle.ones([4, 64], dtype='float32', name='bias') + add_out = paddle.add(matmul_out, bias, name='add_out') + + # add_out -> [memcpy_d2h] -> add_out' -> [sub] -> sub_out -> [tanh] -> tanh_out + with paddle.static.device_guard('cpu'): + sub_out = paddle.subtract(add_out, data, name='sub_out') + tanh_out = paddle.tanh(sub_out, name='tanh_out') + + with paddle.static.device_guard('gpu'): + 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 = paddle.add(out_before, out_last, name='out') + mean = paddle.mean(out, name='mean_out') + + return main_program, startup_program, [mean] + + +class SwitchExecutorInterfaceWithFeed(unittest.TestCase): + def setUp(self): + self.place = ( + paddle.CUDAPlace(0) + if core.is_compiled_with_cuda() + else paddle.CPUPlace() + ) + self.iter_run = 2 + + def build_program(self, is_double=False): + main_program = paddle.static.Program() + startup_program = paddle.static.Program() + with paddle.static.program_guard(main_program, startup_program): + a = paddle.static.data(name="a", shape=[2, 2], dtype='float32') + b = paddle.ones([2, 2]) * 2 + t = paddle.static.nn.fc(a, 2) + c = t + b + if is_double: + c = c + c + + return main_program, startup_program, [c] + + def _run( + self, + feed, + use_str=False, + is_double=False, + add_wrong_fetch=False, + use_compiled=False, + ): + paddle.seed(2020) + + main_program, startup_program, fetch_vars = self.build_program( + is_double + ) + + exe = paddle.static.Executor(self.place) + exe.run(startup_program) + + if use_compiled: + main_program = paddle.static.CompiledProgram(main_program) + + if use_str: # test for fetch name + fetch_vars = [x.name for x in fetch_vars] + if add_wrong_fetch: # test for wrong fetch type + fetch_vars.append(1123) + outs = [] + for i in range(self.iter_run): + out = exe.run(main_program, feed=feed, fetch_list=fetch_vars)[0] + + outs.append(out) + + return outs + + def run_dygraph(self, feed): + def run_once(is_double): + paddle.seed(2020) + a = feed['a'] + a = paddle.to_tensor(a, dtype='float32') + b = paddle.ones([2, 2]) * 2 + t = paddle.nn.Linear(2, 2)(a) + c = t + b + if is_double: + c = c + c + return c.numpy() + + out1 = [] + for i in range(self.iter_run): + out1.append(run_once(False)) + out2 = [] + for i in range(self.iter_run): + out2.append(run_once(True)) + return [out1, out2] + + def run_new_executor(self, feed, use_compiled=False): + # run construct program 1 + out1 = self._run( + feed, use_str=False, is_double=False, use_compiled=use_compiled + ) + # run construct program 2 with same executor + out2 = self._run( + feed, use_str=True, is_double=True, use_compiled=use_compiled + ) + + return [out1, out2] + + def test_with_feed(self): + data = np.ones([2, 2], dtype="float32") + feed = {"a": data, 'fake_input': data} + + with static_guard(): + res = self.run_new_executor(feed) + with paddle.base.dygraph.guard(): + gt = self.run_dygraph(feed) + for x, y in zip(gt, res): + np.testing.assert_array_equal(x, y) + + def test_with_error(self): + feed = [{'a': np.ones([2, 2], dtype="float32")}] + + with self.assertRaises(TypeError): + self._run(feed[0], add_wrong_fetch=True) + + def test_empty_program(self): + program = paddle.static.Program() + exe = paddle.static.Executor(self.place) + for i in range(10): + out = exe.run() # old executor + + for i in range(10): + print(i, flush=1) + out = exe.run(program, feed=None) + + +if __name__ == "__main__": + unittest.main() diff --git a/test/deprecated/standalone_executor/test_standalone_executor_multi_micro_batch.py b/test/deprecated/standalone_executor/test_standalone_executor_multi_micro_batch_deprecated.py similarity index 100% rename from test/deprecated/standalone_executor/test_standalone_executor_multi_micro_batch.py rename to test/deprecated/standalone_executor/test_standalone_executor_multi_micro_batch_deprecated.py diff --git a/test/deprecated/standalone_executor/test_standalone_executor_plan.py b/test/deprecated/standalone_executor/test_standalone_executor_plan_deprecated.py similarity index 100% rename from test/deprecated/standalone_executor/test_standalone_executor_plan.py rename to test/deprecated/standalone_executor/test_standalone_executor_plan_deprecated.py diff --git a/test/deprecated/standalone_executor/test_standalone_op_priority.py b/test/deprecated/standalone_executor/test_standalone_op_priority_deprecated.py similarity index 100% rename from test/deprecated/standalone_executor/test_standalone_op_priority.py rename to test/deprecated/standalone_executor/test_standalone_op_priority_deprecated.py diff --git a/test/deprecated/standalone_executor/test_standalone_sequentail_run.py b/test/deprecated/standalone_executor/test_standalone_sequentail_run_deprecated.py similarity index 100% rename from test/deprecated/standalone_executor/test_standalone_sequentail_run.py rename to test/deprecated/standalone_executor/test_standalone_sequentail_run_deprecated.py diff --git a/test/deprecated/tokenizer/CMakeLists.txt b/test/deprecated/tokenizer/CMakeLists.txt index 1cf384df660b38..674bcb192241e9 100644 --- a/test/deprecated/tokenizer/CMakeLists.txt +++ b/test/deprecated/tokenizer/CMakeLists.txt @@ -8,5 +8,5 @@ foreach(src ${TEST_OPS}) py_test(${src} SRCS ${src}.py) endforeach() -set_tests_properties(test_faster_tokenizer_op PROPERTIES LABELS - "RUN_TYPE=EXCLUSIVE") +set_tests_properties(test_faster_tokenizer_op_deprecated + PROPERTIES LABELS "RUN_TYPE=EXCLUSIVE") diff --git a/test/deprecated/tokenizer/test_faster_tokenizer_op.py b/test/deprecated/tokenizer/test_faster_tokenizer_op_deprecated.py similarity index 100% rename from test/deprecated/tokenizer/test_faster_tokenizer_op.py rename to test/deprecated/tokenizer/test_faster_tokenizer_op_deprecated.py diff --git a/test/legacy_test/CMakeLists.txt b/test/legacy_test/CMakeLists.txt index 2f0e62e82df894..1f4b009bc3b5ef 100644 --- a/test/legacy_test/CMakeLists.txt +++ b/test/legacy_test/CMakeLists.txt @@ -422,6 +422,7 @@ function(parallel_bash_test_modules TARGET_NAME) endif() endfunction() +list(REMOVE_ITEM TEST_OPS test_data_norm_op) list(REMOVE_ITEM TEST_OPS test_warpctc_op) list(REMOVE_ITEM TEST_OPS test_imperative_resnet) list(REMOVE_ITEM TEST_OPS test_imperative_resnet_sorted_gradient) @@ -691,7 +692,7 @@ if(WITH_DISTRIBUTE) endif() endif() endif() - +py_test_modules(test_data_norm_op MODULES test_data_norm_op) py_test_modules( test_fuse_bn_add_act_pass MODULES @@ -799,6 +800,8 @@ if(WITH_NV_JETSON) set_tests_properties(test_batch_norm_op_prim_nchw PROPERTIES TIMEOUT 1500) set_tests_properties(test_batch_norm_op_prim_nhwc PROPERTIES TIMEOUT 1500) set_tests_properties(test_pool3d_op PROPERTIES TIMEOUT 1500) + set_tests_properties(test_conv3d_transpose_part2_op PROPERTIES TIMEOUT 1200) + set_tests_properties(test_layer_norm_op PROPERTIES TIMEOUT 1500) else() set_tests_properties(test_concat_op PROPERTIES TIMEOUT 400) set_tests_properties(test_conv3d_op PROPERTIES TIMEOUT 120) @@ -806,6 +809,8 @@ else() set_tests_properties(test_batch_norm_op_prim_nchw PROPERTIES TIMEOUT 250) set_tests_properties(test_batch_norm_op_prim_nhwc PROPERTIES TIMEOUT 250) set_tests_properties(test_pool3d_op PROPERTIES TIMEOUT 150) + set_tests_properties(test_conv3d_transpose_part2_op PROPERTIES TIMEOUT 120) + set_tests_properties(test_layer_norm_op PROPERTIES TIMEOUT 250) endif() if((WITH_GPU OR WITH_ROCM) AND (LINUX)) py_test_modules(test_conv3d_transpose_op MODULES test_conv3d_transpose_op @@ -959,6 +964,7 @@ endif() set(TEST_CINN_OPS test_assign_op + test_layer_norm_op test_atan2_op test_cast_op test_stack_op @@ -1028,6 +1034,9 @@ set_tests_properties( # 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_adagrad_op + test_batch_norm_op + test_nce + test_layer_norm_op test_eigh_op test_matmul_op test_matmul_v2_op @@ -1176,3 +1185,12 @@ set_tests_properties(test_matmul_op_static_build PROPERTIES TIMEOUT 120) set_tests_properties(test_matmul_v2_op_static_build PROPERTIES TIMEOUT 120) set_tests_properties(test_paddle_save_load_binary_static_build PROPERTIES TIMEOUT 120) + +set_tests_properties(test_argsort_op PROPERTIES TIMEOUT 120) +set_tests_properties(test_conv_nn_grad PROPERTIES LABELS "RUN_TYPE=EXCLUSIVE") +set_tests_properties(test_conv_nn_grad PROPERTIES TIMEOUT 220) +set_tests_properties(test_data_norm_op PROPERTIES LABELS "RUN_TYPE=DIST") +set_tests_properties(test_deformable_conv_op PROPERTIES TIMEOUT 200) +set_tests_properties(test_regularizer PROPERTIES TIMEOUT 150) +set_tests_properties(test_regularizer_api PROPERTIES TIMEOUT 150) +set_tests_properties(test_sgd_op PROPERTIES TIMEOUT 250) diff --git a/test/deprecated/legacy_test/test_adamax_api.py b/test/legacy_test/test_adamax_api.py similarity index 66% rename from test/deprecated/legacy_test/test_adamax_api.py rename to test/legacy_test/test_adamax_api.py index 1fc1878d819957..a995659df4c100 100644 --- a/test/deprecated/legacy_test/test_adamax_api.py +++ b/test/legacy_test/test_adamax_api.py @@ -17,7 +17,6 @@ import numpy as np import paddle -from paddle import base class TestAdamaxAPI(unittest.TestCase): @@ -36,34 +35,6 @@ def test_adamax_api_dygraph(self): adam.step() adam.clear_gradients() - def test_adamax_api(self): - paddle.enable_static() - place = base.CPUPlace() - shape = [2, 3, 8, 8] - 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) - beta1 = 0.85 - beta2 = 0.95 - opt = paddle.optimizer.Adamax( - learning_rate=1e-5, - beta1=beta1, - beta2=beta2, - weight_decay=0.01, - epsilon=1e-8, - ) - opt.minimize(loss) - - exe.run(startup) - data_np = np.random.random(shape).astype('float32') - rets = exe.run(train_prog, feed={"data": data_np}, fetch_list=[loss]) - assert rets[0] is not None - class TestAdamaxAPIGroup(TestAdamaxAPI): def test_adamax_api_dygraph(self): diff --git a/test/deprecated/legacy_test/test_argsort_op.py b/test/legacy_test/test_argsort_op.py similarity index 68% rename from test/deprecated/legacy_test/test_argsort_op.py rename to test/legacy_test/test_argsort_op.py index 58597766644f57..e1786ada841bc6 100644 --- a/test/deprecated/legacy_test/test_argsort_op.py +++ b/test/legacy_test/test_argsort_op.py @@ -20,9 +20,6 @@ import paddle 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 from paddle.pir_utils import test_with_pir_api np.random.seed(123) @@ -66,293 +63,6 @@ def create_tensor(np_data, place): return tensor -class TestArgsortOpCPU(unittest.TestCase): - def setup_program(self): - self.main_program = Program() - self.startup_program = Program() - self.init_place() - - def setUp(self): - paddle.enable_static() - self.init_axis() - self.init_datatype() - self.init_direction() - self.init_inputshape() - - self.setup_program() - self.feed_data_field = {"x", "label"} - self.grad_data_field = {"x"} - - self.py_argsort = PyArgsort( - self.input_shape, self.axis, self.descending, self.dtype - ) - - 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 - ) - x.stop_gradient = False - x.desc.set_need_check_feed(False) - label = paddle.static.data( - name="label", - shape=[-1] + list(self.input_shape), - dtype=self.dtype, - ) - label.desc.set_need_check_feed(False) - self.index = paddle.argsort( - x=x, axis=self.axis, descending=self.descending - ) - self.sorted_x = paddle.sort( - x=x, axis=self.axis, descending=self.descending - ) - self.sorted_x.stop_gradient = False - loss = paddle.multiply(self.sorted_x, label) - self.loss = paddle.sum(loss) - - def forward(self): - self.feed_map = { - x: create_tensor(getattr(self.py_argsort, x), self.place) - for x in self.feed_data_field - } - exe = Executor(self.place) - out = exe.run( - self.main_program, - feed=self.feed_map, - fetch_list=[self.index, self.sorted_x, self.loss], - ) - return out - - def backward(self): - self.feed_map = { - x: create_tensor(getattr(self.py_argsort, x), self.place) - for x in self.feed_data_field - } - fetch_list = [ - self.main_program.global_block().var(grad_var_name(x)) - for x in self.grad_data_field - ] - exe = Executor(self.place) - out = exe.run( - self.main_program, - feed=self.feed_map, - fetch_list=fetch_list, - return_numpy=False, - ) - return out - - def test_backward(self, numeric_grad_delta=1e-5, max_relative_error=1e-7): - self.check_forward() - - with base.program_guard(self.main_program, self.startup_program): - append_backward(self.loss) - - ana_grad = [np.array(x) for x in self.backward()] - - num_grad = self.get_numerical_gradient(delta=numeric_grad_delta) - self.assert_is_close( - num_grad, - ana_grad, - 'x', - max_relative_error=max_relative_error, - msg_prefix="Gradient Check On %s" % str(self.place), - ) - - def check_forward(self): - pd_outputs = self.forward() - py_outputs = self.py_argsort.forward() - for pd_output, py_output in zip(pd_outputs, py_outputs): - self.assertEqual(pd_output.shape, py_output.shape) - np.testing.assert_allclose( - pd_output, py_output, rtol=1e-05, atol=0, equal_nan=False - ) - - def get_numerical_gradient(self, delta=1e-7): - if self.dtype == 'float16': - delta = np.array(delta).astype(np.float16) - feed_list = [getattr(self.py_argsort, x) for x in self.grad_data_field] - grad_list = [np.zeros_like(x) for x in feed_list] - for feed, grad in zip(feed_list, grad_list): - for f, g in np.nditer([feed, grad], op_flags=['readwrite']): - o = float(f) - f[...] = o + delta - y_pos = self.forward()[2] - - f[...] = o - delta - y_neg = self.forward()[2] - - f[...] = o - dout_dfeed = (y_pos - y_neg) / (delta * 2) - g[...] = dout_dfeed - - return grad_list - - def assert_is_close( - self, - numeric_grads, - analytic_grads, - names, - max_relative_error, - msg_prefix, - ): - for a, b, name in zip(numeric_grads, analytic_grads, names): - abs_a = np.abs(a) - abs_a[abs_a < 1e-3] = 1 - - diff_mat = np.abs(a - b) / abs_a - max_diff = np.max(diff_mat) - - def err_msg(): - offset = np.argmax(diff_mat > max_relative_error) - return ( - "%s error, %s variable %s max gradient diff %f over limit %f, " - "the first error element is %d, expected %f, but got %f." - ) % ( - 'argsort', - msg_prefix, - name, - max_diff, - max_relative_error, - offset, - a.flatten()[offset], - b.flatten()[offset], - ) - - self.assertLessEqual(max_diff, max_relative_error, err_msg()) - - def init_axis(self): - self.axis = -1 - - def init_datatype(self): - self.dtype = "float64" - - def init_direction(self): - self.descending = False - - def init_inputshape(self): - self.input_shape = (2, 2, 2, 2, 3) - - def init_place(self): - self.place = core.CPUPlace() - - -class TestArgsortOpGPU(TestArgsortOpCPU): - def init_place(self): - if core.is_compiled_with_cuda(): - self.place = core.CUDAPlace(0) - else: - self.place = core.CPUPlace() - - -class TestArgsortOpAxis0CPU(TestArgsortOpCPU): - def init_axis(self): - self.axis = 0 - - -class TestArgsortOpAxis0GPU(TestArgsortOpGPU): - def init_axis(self): - self.axis = 0 - - -class TestArgsortOpAxis1CPU(TestArgsortOpCPU): - def init_axis(self): - self.axis = 1 - - -class TestArgsortOpAxis1GPU(TestArgsortOpGPU): - def init_axis(self): - self.axis = 1 - - -class TestArgsortOpAxis2CPU(TestArgsortOpCPU): - def init_axis(self): - self.axis = 2 - - -class TestArgsortOpAxis2GPU(TestArgsortOpGPU): - def init_axis(self): - self.axis = 2 - - -class TestArgsortOpAxisNeg1CPU(TestArgsortOpCPU): - def init_axis(self): - self.axis = -1 - - -class TestArgsortOpAxisNeg1GPU(TestArgsortOpGPU): - def init_axis(self): - self.axis = -1 - - -class TestArgsortOpAxisNeg2CPU(TestArgsortOpCPU): - def init_axis(self): - self.axis = -2 - - -class TestArgsortOpAxisNeg2GPU(TestArgsortOpGPU): - def init_axis(self): - self.axis = -2 - - -class TestArgsortOpDescendingAxisCPU(TestArgsortOpCPU): - def init_direction(self): - self.descending = True - - -class TestArgsortOpDescendingAxisGPU(TestArgsortOpGPU): - def init_direction(self): - self.descending = True - - -class TestArgsortOpDescendingAxis0CPU(TestArgsortOpAxis0CPU): - def init_direction(self): - self.descending = True - - -class TestArgsortOpDescendingAxis0GPU(TestArgsortOpAxis0GPU): - def init_direction(self): - self.descending = True - - -class TestArgsortOpDescendingAxis1CPU(TestArgsortOpAxis1CPU): - def init_direction(self): - self.descending = True - - -class TestArgsortOpDescendingAxis1GPU(TestArgsortOpAxis1GPU): - def init_direction(self): - self.descending = True - - -class TestArgsortOpDescendingAxis2CPU(TestArgsortOpAxis2CPU): - def init_direction(self): - self.descending = True - - -class TestArgsortOpDescendingAxis2GPU(TestArgsortOpAxis2GPU): - def init_direction(self): - self.descending = True - - -class TestArgsortOpDescendingAxisNeg1CPU(TestArgsortOpAxisNeg1CPU): - def init_direction(self): - self.descending = True - - -class TestArgsortOpDescendingAxisNeg1GPU(TestArgsortOpAxisNeg1GPU): - def init_direction(self): - self.descending = True - - -class TestArgsortOpDescendingAxisNeg2CPU(TestArgsortOpAxisNeg2CPU): - def init_direction(self): - self.descending = True - - -class TestArgsortOpDescendingAxisNeg2GPU(TestArgsortOpAxisNeg2GPU): - def init_direction(self): - self.descending = True - - class TestArgsortErrorOnCPU(unittest.TestCase): def setUp(self): self.place = core.CPUPlace() diff --git a/test/legacy_test/test_backward.py b/test/legacy_test/test_backward.py new file mode 100644 index 00000000000000..05fdb572c79de2 --- /dev/null +++ b/test/legacy_test/test_backward.py @@ -0,0 +1,106 @@ +# 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. + +import unittest + +import numpy as np + +import paddle +from paddle.base import backward + + +class BackwardNet: + """ + Abstract Base Class. + All Net inherited this Class should implement two functions: + build_model: build net to test the logic of backward + init_data: fake input data to test all programs. + """ + + def __init__(self): + self.stop_gradient_grad_vars = set() + self.no_grad_vars = set() + self.params_names = set() + self.op_path = [] + + def build_model(self): + """ + Build net to test the logic of backward. + :return: loss + """ + raise NotImplementedError + + def init_data(self): + """ + Fake input data to test all programs. + :return: dict, {'var_name': var_data} + """ + raise NotImplementedError + + +# TODO(Aurelius84): add conditional network test +class ConditionalNet(BackwardNet): + def __init__(self): + super().__init__() + + +class TestBackwardUninitializedVariable(unittest.TestCase): + """this case is found in yolov5 while to_static. + gradient aggregation may cause sum a invalid variable. + """ + + def test(self): + paddle.enable_static() + main_prg, startup_prg = paddle.static.Program(), paddle.static.Program() + with paddle.static.program_guard(main_prg, startup_prg): + gt = paddle.static.data(name='gt', shape=[4], dtype='float32') + x = paddle.static.data(name='x', shape=[2], dtype='float32') + gt.stop_gradient = True + x.stop_gradient = False + gt = gt.reshape([4, 1]).reshape([4]) + loss = ( + paddle.nn.functional.binary_cross_entropy(x, gt[:2]) + + (gt[2:4] * x).sum() + ) + exe = paddle.static.Executor() + paddle.base.backward.gradients(loss, []) + exe.run(startup_prg) + # Optimizer + out = exe.run( + main_prg, + feed={ + 'gt': np.array([1.0, 1.0, 0.0, 0.0], dtype='float32'), + 'x': np.array([0.5, 0.5], dtype='float32'), + }, + fetch_list=[loss], + ) + print(out) + + +class TestStripGradSuffix(unittest.TestCase): + def test_strip_grad_suffix(self): + cases = ( + ('x@GRAD', 'x'), + ('x@GRAD@GRAD', 'x'), + ('x@GRAD@RENAME@1', 'x'), + ('x@GRAD_slice_0@GRAD', 'x@GRAD_slice_0'), + ('grad/grad/x@GRAD@RENAME@block0@1@GRAD', 'x'), + ) + for input_, desired in cases: + self.assertEqual(backward._strip_grad_suffix_(input_), desired) + + +if __name__ == '__main__': + paddle.enable_static() + unittest.main() diff --git a/test/deprecated/legacy_test/test_batch_norm_op.py b/test/legacy_test/test_batch_norm_op.py similarity index 63% rename from test/deprecated/legacy_test/test_batch_norm_op.py rename to test/legacy_test/test_batch_norm_op.py index 63893a4353a3c7..445c2082d13cd9 100644 --- a/test/deprecated/legacy_test/test_batch_norm_op.py +++ b/test/legacy_test/test_batch_norm_op.py @@ -26,7 +26,7 @@ import paddle from paddle import base -from paddle.base import Program, core, program_guard +from paddle.base import core from paddle.base.framework import grad_var_name from paddle.pir_utils import test_with_pir_api @@ -545,402 +545,6 @@ def test_check_output(self): ) -class TestBatchNormOpTraining(unittest.TestCase): - def setUp(self): - self.use_mkldnn = False - self.fuse_with_relu = False - self.data_formats = ["NCHW", "NHWC"] - self.momentum = 0.9 - self.use_momentum_variable = False - self.epsilon = 0.00001 - self.init_kernel_type() - self.init_test_case() - - def init_test_case(self): - self.use_global_stats = False - self.no_grad_set = set() - self.fetch_list = [ - 'y', - 'mean', - 'variance', - 'saved_mean', - 'saved_variance', - 'x@GRAD', - 'scale@GRAD', - 'bias@GRAD', - ] - - def __assert_close(self, tensor, np_array, msg, atol=1e-4): - np.allclose(np.array(tensor), np_array, atol=atol) - - def ref_forward_backward( - self, - x, - y_grad, - scale, - bias, - mean, - variance, - epsilon, - momentum, - shape, - data_layout, - ): - # run forward - y, saved_mean, var_ref = _reference_training( - x, scale, bias, epsilon, data_layout - ) - mean_out = saved_mean * (1.0 - momentum) + momentum * mean - variance_out = var_ref * (1.0 - momentum) + momentum * variance - saved_variance = 1.0 / np.sqrt(var_ref + epsilon) - # run backward - x_grad, scale_grad, bias_grad = _reference_grad( - x, y_grad, scale, saved_mean, var_ref, epsilon, data_layout - ) - - return ( - y, - mean_out, - variance_out, - saved_mean, - saved_variance, - x_grad, - scale_grad, - bias_grad, - ) - - def set_mean_variance(self, scale_shape, x, data_layout): - mean, variance = _cal_mean_variance(x, self.epsilon, data_layout) - mean_pre = np.zeros(scale_shape).astype(np.float32) - variance_pre = np.ones(scale_shape).astype(np.float32) - # computing global mean/variance for one step - if self.use_global_stats: - mom = self.momentum - mean = mean * (1.0 - mom) + mom * mean_pre - variance = variance * (1.0 - mom) + mom * variance_pre - return mean, variance - - def test_forward_backward(self): - def test_with_place(place, data_layout, shape): - # attr - epsilon = self.epsilon - momentum = self.momentum - if data_layout == "NCHW": - n, c, h, w = shape[0], shape[1], shape[2], shape[3] - else: - n, h, w, c = shape[0], shape[1], shape[2], shape[3] - scale_shape = [c] - - np.random.seed(123) - x = np.random.random_sample(shape).astype(np.float32) - scale = np.random.random_sample(scale_shape).astype(np.float32) - bias = np.random.random_sample(scale_shape).astype(np.float32) - mean, variance = self.set_mean_variance(scale_shape, x, data_layout) - y_grad = np.random.random_sample(shape).astype(np.float32) - momentum_var = np.array([momentum]).astype(np.float32) - - ( - y, - mean_out, - variance_out, - saved_mean, - saved_variance, - x_grad, - scale_grad, - bias_grad, - ) = self.ref_forward_backward( - x, - y_grad, - scale, - bias, - mean, - variance, - epsilon, - momentum, - shape, - data_layout, - ) - - var_dict = locals() - var_dict['y@GRAD'] = y_grad - var_dict['x@GRAD'] = x_grad - var_dict['scale@GRAD'] = scale_grad - var_dict['bias@GRAD'] = bias_grad - - var_names = [ - 'x', - 'scale', - 'bias', - 'mean', - 'variance', - 'y', - 'saved_mean', - 'saved_variance', - 'momentum_var', - ] - ground_truth = {name: var_dict[name] for name in var_names} - - program = base.Program() - with base.program_guard(program): - block = program.global_block() - for name in ground_truth: - block.create_var( - name=name, - dtype='float32', - shape=ground_truth[name].shape, - ) - inputs = { - "X": block.var('x'), - "Scale": block.var('scale'), - "Bias": block.var('bias'), - "Mean": block.var('mean'), - "Variance": block.var('variance'), - } - attrs = { - "epsilon": epsilon, - "is_test": False, - "data_layout": data_layout, - "use_mkldnn": self.use_mkldnn, - "fuse_with_relu": self.fuse_with_relu, - "use_global_stats": self.use_global_stats, - } - if self.use_momentum_variable: - inputs['MomentumTensor'] = block.var('momentum_var') - else: - attrs['momentum'] = momentum - - outputs = { - "Y": block.var('y'), - "MeanOut": block.var('mean'), # share memory - "VarianceOut": block.var('variance'), # share memory - "SavedMean": block.var('saved_mean'), - "SavedVariance": block.var('saved_variance'), - } - block.create_var(name="reserve_space", dtype='float32') - outputs["ReserveSpace"] = block.var('reserve_space') - bn_op = block.append_op( - type="batch_norm", - inputs=inputs, - outputs=outputs, - attrs=attrs, - ) - block.create_var(name='y@GRAD', dtype='float32', shape=y.shape) - - # generate backward op_desc - grad_op_desc_list, op_grad_to_var = core.get_grad_op_desc( - bn_op.desc, self.no_grad_set, [] - ) - grad_op_desc = grad_op_desc_list[0] - new_op_desc = block.desc.append_op() - new_op_desc.copy_from(grad_op_desc) - for var_name in grad_op_desc.output_arg_names(): - block.desc.var(var_name.encode("ascii")) - grad_op_desc.infer_var_type(block.desc) - grad_op_desc.infer_shape(block.desc) - for arg in grad_op_desc.output_arg_names(): - grad_var = block.desc.find_var(arg.encode("ascii")) - grad_var.set_dtype(core.VarDesc.VarType.FP32) - - program._sync_with_cpp() - - exe = base.Executor(place) - out = exe.run( - program, - feed={ - name: var_dict[name] - for name in [ - 'x', - 'scale', - 'bias', - 'mean', - 'variance', - 'y@GRAD', - 'momentum_var', - ] - }, - fetch_list=self.fetch_list, - ) - - for id, name in enumerate(self.fetch_list): - if name == 'variance': - self.__assert_close( - var_dict[name], out[id], name, atol=1e-3 - ) - continue - self.__assert_close(var_dict[name], out[id], name) - print("op test forward passed: ", str(place), data_layout) - - places = [core.CPUPlace()] - - if core.is_compiled_with_cuda(): - places.append(core.CUDAPlace(0)) - - for place in places: - for data_format in self.data_formats: - test_with_place(place, data_format, [2, 3, 4, 5]) - - def init_kernel_type(self): - pass - - -class TestBatchNormOpTrainingCase1(TestBatchNormOpTraining): - def init_test_case(self): - self.use_global_stats = False - self.no_grad_set = {'scale@GRAD', 'bias@GRAD'} - self.fetch_list = ['y', 'mean', 'variance', 'x@GRAD'] - - -class TestBatchNormOpTrainingCase2(TestBatchNormOpTraining): - def init_test_case(self): - self.use_global_stats = False - self.no_grad_set = set() - self.fetch_list = [ - 'y', - 'mean', - 'variance', - 'saved_mean', - 'saved_variance', - 'x@GRAD', - 'scale@GRAD', - 'bias@GRAD', - ] - os.environ['FLAGS_cudnn_batchnorm_spatial_persistent'] = "1" - - -class TestBatchNormOpTrainingCase3(TestBatchNormOpTraining): - def init_test_case(self): - self.use_global_stats = False - self.no_grad_set = {'x@GRAD'} - self.fetch_list = ['y', 'mean', 'variance', 'scale@GRAD', 'bias@GRAD'] - - -class TestBatchNormOpTrainingMomentumVariable(TestBatchNormOpTraining): - def init_test_case(self): - self.use_momentum_variable = True - self.use_global_stats = False - self.no_grad_set = set() - self.fetch_list = [ - 'y', - 'mean', - 'variance', - 'saved_mean', - 'saved_variance', - 'x@GRAD', - 'scale@GRAD', - 'bias@GRAD', - ] - - -class TestBatchNormOpFreezeStatsTraining(TestBatchNormOpTraining): - def init_test_case(self): - self.use_global_stats = True - self.no_grad_set = set() - self.fetch_list = [ - 'y', - 'mean', - 'variance', - 'x@GRAD', - 'scale@GRAD', - 'bias@GRAD', - ] - - def reference_grad(self, x, y_grad, scale, mean, var, epsilon, data_format): - if data_format == "NCHW": - x = np.transpose(x, (0, 2, 3, 1)) - y_grad = np.transpose(y_grad, (0, 2, 3, 1)) - - x_grad = scale * y_grad / np.sqrt(var + epsilon) - grad_scale = np.sum( - y_grad * (x - mean) / np.sqrt(var + epsilon), axis=(0, 1, 2) - ) - grad_offset = np.sum(y_grad, axis=(0, 1, 2)) - - # transfer back to N, C, H, W - if data_format == "NCHW": - x_grad = np.transpose(x_grad, (0, 3, 1, 2)) - x = np.transpose(x, (0, 3, 1, 2)) - y_grad = np.transpose(y_grad, (0, 3, 1, 2)) - - return x_grad, grad_scale, grad_offset - - def ref_forward_backward( - self, - x, - y_grad, - scale, - bias, - mean, - variance, - epsilon, - momentum, - shape, - data_layout, - ): - if data_layout != "NCHW" and data_layout != "NHWC": - raise ValueError("Unknown data order.") - - if data_layout == "NCHW": - x = np.transpose(x, (0, 2, 3, 1)) - - # run normalizaton - normalized = (x - mean) / np.sqrt(variance + epsilon) - y = normalized * scale + bias - - # transfer back to N, C, H, W - if data_layout == "NCHW": - x = np.transpose(x, (0, 3, 1, 2)) - y = np.transpose(y, (0, 3, 1, 2)) - - mean_out = mean - variance_out = variance - saved_variance = 1.0 / np.sqrt(variance + epsilon) - # run backward - x_grad, scale_grad, bias_grad = self.reference_grad( - x, y_grad, scale, mean, variance, epsilon, data_layout - ) - - return ( - y, - mean_out, - variance_out, - mean, - saved_variance, - x_grad, - scale_grad, - bias_grad, - ) - - -class TestBatchNormOpFreezeStatsAndScaleBiasTraining( - TestBatchNormOpFreezeStatsTraining -): - def init_test_case(self): - self.use_global_stats = True - self.no_grad_set = {'scale@GRAD', 'bias@GRAD'} - self.fetch_list = ['y', 'mean', 'variance', 'x@GRAD'] - - -class TestBatchNormOpError(unittest.TestCase): - def test_errors(self): - with program_guard(Program(), Program()): - # the input of batch_norm must be Variable. - 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) - - # the input dtype of batch_norm must be float16 or float32 or float64 - # float16 only can be set on GPU place - x2 = paddle.static.data( - name='x2', shape=[-1, 3, 4, 5, 6], dtype="int32" - ) - self.assertRaises(TypeError, paddle.static.nn.batch_norm, x2) - - # the first dimension of input for batch_norm must between [2d, 5d]. - x3 = paddle.static.data("", shape=[0], dtype="float32") - self.assertRaises(ValueError, paddle.static.nn.batch_norm, x3) - - class TestDygraphBatchNormAPIError(unittest.TestCase): @test_with_pir_api def test_errors(self): diff --git a/test/legacy_test/test_bilinear_tensor_product_op.py b/test/legacy_test/test_bilinear_tensor_product_op.py new file mode 100644 index 00000000000000..8a74e5c2bdfbf2 --- /dev/null +++ b/test/legacy_test/test_bilinear_tensor_product_op.py @@ -0,0 +1,57 @@ +# Copyright (c) 2018 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 op_test import OpTest + +import paddle +from paddle import base + + +class TestBilinearTensorProductOp(OpTest): + def setUp(self): + self.op_type = "bilinear_tensor_product" + self.python_api = paddle.nn.functional.bilinear + batch_size = 6 + size0 = 5 + size1 = 4 + size2 = 5 + 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) + bias = np.random.random((1, size2)).astype(dtype) + output = np.zeros((batch_size, size2)).astype(dtype) + for i in range(size2): + w_i = w[i, :, :] + output[:, i] = np.sum(np.matmul(a, w_i) * b, axis=1) + self.inputs = { + 'X': a, + 'Y': b, + 'Weight': w, + 'Bias': bias, + } + self.outputs = {'Out': output + bias} + + def test_check_output(self): + self.check_output() + + def test_check_grad_normal(self): + self.check_grad(['X', 'Y', 'Weight', 'Bias'], 'Out') + + +if __name__ == "__main__": + unittest.main() diff --git a/test/legacy_test/test_conv3d_transpose_part2_op.py b/test/legacy_test/test_conv3d_transpose_part2_op.py new file mode 100644 index 00000000000000..da75a5720a80d8 --- /dev/null +++ b/test/legacy_test/test_conv3d_transpose_part2_op.py @@ -0,0 +1,104 @@ +# Copyright (c) 2018 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 + +sys.path.append("../../legacy_test") +from test_conv3d_transpose_op import ( + TestConv3DTransposeOp, + create_test_cudnn_bf16_class, + create_test_cudnn_fp16_class, +) + + +class TestWithSymmetricPad_NHWC(TestConv3DTransposeOp): + def init_test_case(self): + self.pad = [1, 1, 1] + self.stride = [1, 1, 1] + self.dilations = [1, 1, 1] + self.groups = 1 + self.input_size = [2, 5, 5, 5, 3] # NDHWC + f_c = self.input_size[-1] + self.filter_size = [f_c, 6, 3, 3, 3] + self.data_format = 'NHWC' + + +class TestWithAsymmetricPad_NHWC(TestConv3DTransposeOp): + def init_test_case(self): + self.pad = [1, 0, 1, 0, 1, 2] + self.stride = [1, 1, 1] + self.dilations = [1, 1, 1] + self.groups = 1 + self.input_size = [2, 5, 5, 5, 3] # NDHWC + f_c = self.input_size[-1] + self.filter_size = [f_c, 6, 3, 3, 3] + self.data_format = 'NHWC' + + +class TestWithGroups_NHWC(TestConv3DTransposeOp): + def init_test_case(self): + self.check_no_filter = True + self.pad = [1, 1, 1] + self.stride = [1, 1, 1] + self.dilations = [1, 1, 1] + self.groups = 2 + self.input_size = [2, 5, 5, 5, 4] # NDHWC + f_c = self.input_size[-1] + self.filter_size = [f_c, 3, 3, 3, 3] + self.data_format = 'NHWC' + + +class TestWithStride_NHWC(TestConv3DTransposeOp): + def init_test_case(self): + self.pad = [1, 1, 1] + self.stride = [2, 2, 2] + self.dilations = [1, 1, 1] + self.groups = 1 + self.input_size = [2, 5, 5, 5, 3] # NCDHW + f_c = self.input_size[-1] + self.filter_size = [f_c, 6, 3, 3, 3] + self.data_format = 'NHWC' + + +class TestWithDilation_NHWC(TestConv3DTransposeOp): + def init_test_case(self): + self.check_no_input = True + self.pad = [1, 1, 1] + self.stride = [1, 1, 1] + self.dilations = [2, 2, 2] + self.groups = 1 + self.input_size = [2, 5, 5, 5, 3] # NCDHW + f_c = self.input_size[-1] + self.filter_size = [f_c, 6, 3, 3, 3] + self.data_format = 'NHWC' + + +# ----------------Conv3DTransposeCUDNN fp16---------------- +create_test_cudnn_fp16_class(TestWithSymmetricPad_NHWC) +create_test_cudnn_fp16_class(TestWithAsymmetricPad_NHWC) +create_test_cudnn_fp16_class(TestWithGroups_NHWC) +create_test_cudnn_fp16_class(TestWithStride_NHWC) +create_test_cudnn_fp16_class(TestWithDilation_NHWC) + + +# ----------------Conv3DTransposeCUDNN bf16---------------- +create_test_cudnn_bf16_class(TestWithSymmetricPad_NHWC) +create_test_cudnn_bf16_class(TestWithAsymmetricPad_NHWC) +create_test_cudnn_bf16_class(TestWithGroups_NHWC) +create_test_cudnn_bf16_class(TestWithStride_NHWC) +create_test_cudnn_bf16_class(TestWithDilation_NHWC) + +if __name__ == '__main__': + unittest.main() diff --git a/test/deprecated/legacy_test/test_conv_nn_grad.py b/test/legacy_test/test_conv_nn_grad.py similarity index 58% rename from test/deprecated/legacy_test/test_conv_nn_grad.py rename to test/legacy_test/test_conv_nn_grad.py index 40152a181f1c6c..58461aefcd9aba 100644 --- a/test/deprecated/legacy_test/test_conv_nn_grad.py +++ b/test/legacy_test/test_conv_nn_grad.py @@ -26,23 +26,6 @@ class TestConvDoubleGradCheck(unittest.TestCase): - @prog_scope() - def func(self, place): - shape = [2, 4, 3, 3] - eps = 0.005 - 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 = 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)) - gradient_checker.double_grad_check( - [x] + w, y, x_init=[x_arr] + w_arr, place=place, eps=eps - ) - @test_with_pir_api @prog_scope() def func_pir(self, place): @@ -66,28 +49,10 @@ def test_grad(self): if core.is_compiled_with_cuda(): places.append(base.CUDAPlace(0)) for p in places: - self.func(p) self.func_pir(p) class TestConvDoubleGradCheckTest0(unittest.TestCase): - @prog_scope() - def func(self, place): - shape = [2, 4, 3, 3] - eps = 0.005 - 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 = 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)) - gradient_checker.double_grad_check( - [x] + w, y, x_init=[x_arr] + w_arr, place=place, eps=eps - ) - @test_with_pir_api @prog_scope() def func_pir(self, place): @@ -111,28 +76,10 @@ def test_grad(self): if core.is_compiled_with_cuda(): places.append(base.CUDAPlace(0)) for p in places: - self.func(p) self.func_pir(p) class TestConvDoubleGradCheckTest1(unittest.TestCase): - @prog_scope() - def func(self, place): - shape = [2, 3, 3, 3] - eps = 0.005 - 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 = 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)) - gradient_checker.double_grad_check( - [x] + w, y, x_init=[x_arr] + w_arr, place=place, eps=eps - ) - @test_with_pir_api @prog_scope() def func_pir(self, place): @@ -156,28 +103,10 @@ def test_grad(self): if core.is_compiled_with_cuda(): places.append(base.CUDAPlace(0)) for p in places: - self.func(p) self.func_pir(p) class TestConv3DDoubleGradCheck(unittest.TestCase): - @prog_scope() - def func(self, place): - shape = [2, 4, 3, 4, 2] - eps = 0.005 - 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 = 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)) - gradient_checker.double_grad_check( - [x] + w, y, x_init=[x_arr] + w_arr, place=place, eps=eps - ) - @test_with_pir_api @prog_scope() def func_pir(self, place): @@ -201,28 +130,10 @@ def test_grad(self): if core.is_compiled_with_cuda(): places.append(base.CUDAPlace(0)) for p in places: - self.func(p) self.func_pir(p) class TestConv3DDoubleGradCheckTest1(unittest.TestCase): - @prog_scope() - def func(self, place): - shape = [2, 4, 5, 3, 2] - eps = 0.005 - 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 = 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)) - gradient_checker.double_grad_check( - [x] + w, y, x_init=[x_arr] + w_arr, place=place, eps=eps - ) - @test_with_pir_api @prog_scope() def func_pir(self, place): @@ -246,35 +157,10 @@ def test_grad(self): if core.is_compiled_with_cuda(): places.append(base.CUDAPlace(0)) for p in places: - self.func(p) self.func_pir(p) class TestConv2DoubleGradCheck_AsyPadding(unittest.TestCase): - @prog_scope() - def func(self, place): - shape = [2, 2, 3, 3] - eps = 0.005 - 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, - num_filters=2, - filter_size=1, - padding=[1, 0, 0, 1], - bias_attr=False, - use_cudnn=True, - ) - x_arr = np.random.uniform(-1, 1, shape).astype(dtype) - - 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)) - gradient_checker.double_grad_check( - [x] + w, y, x_init=[x_arr] + w_arr, place=place, eps=eps - ) - @test_with_pir_api @prog_scope() def func_pir(self, place): @@ -298,35 +184,10 @@ def test_grad(self): if core.is_compiled_with_cuda(): places.append(base.CUDAPlace(0)) for p in places: - self.func(p) self.func_pir(p) class TestConv2DoubleGradCheck_PaddingSAME(unittest.TestCase): - @prog_scope() - def func(self, place): - shape = [2, 2, 3, 3] - eps = 0.005 - 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, - num_filters=2, - filter_size=1, - padding="SAME", - bias_attr=False, - use_cudnn=True, - ) - x_arr = np.random.uniform(-1, 1, shape).astype(dtype) - - 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)) - gradient_checker.double_grad_check( - [x] + w, y, x_init=[x_arr] + w_arr, place=place, eps=eps - ) - @test_with_pir_api @prog_scope() def func_pir(self, place): @@ -350,35 +211,10 @@ def test_grad(self): if core.is_compiled_with_cuda(): places.append(base.CUDAPlace(0)) for p in places: - self.func(p) self.func_pir(p) class TestConv2DoubleGradCheck_PaddingVALID(unittest.TestCase): - @prog_scope() - def func(self, place): - shape = [2, 2, 3, 3] - eps = 0.005 - 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, - num_filters=2, - filter_size=1, - padding="VALID", - bias_attr=False, - use_cudnn=True, - ) - x_arr = np.random.uniform(-1, 1, shape).astype(dtype) - - 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)) - gradient_checker.double_grad_check( - [x] + w, y, x_init=[x_arr] + w_arr, place=place, eps=eps - ) - @test_with_pir_api @prog_scope() def func_pir(self, place): @@ -402,37 +238,10 @@ def test_grad(self): if core.is_compiled_with_cuda(): places.append(base.CUDAPlace(0)) for p in places: - self.func(p) self.func_pir(p) class TestConv2DoubleGradCheck_ChannelLast(unittest.TestCase): - @prog_scope() - def func(self, place): - shape = [2, 2, 3, 3] - eps = 0.005 - 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, - num_filters=2, - filter_size=1, - padding=[1, 1], - bias_attr=False, - use_cudnn=True, - groups=1, - data_format="NHWC", - ) - x_arr = np.random.uniform(-1, 1, shape).astype(dtype) - - 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)) - gradient_checker.double_grad_check( - [x] + w, y, x_init=[x_arr] + w_arr, place=place, eps=eps - ) - @test_with_pir_api @prog_scope() def func_pir(self, place): @@ -457,37 +266,10 @@ def test_grad(self): if core.is_compiled_with_cuda(): places.append(base.CUDAPlace(0)) for p in places: - self.func(p) self.func_pir(p) class TestConv2DoubleGradCheck_ChannelLast_AsyPadding(unittest.TestCase): - @prog_scope() - def func(self, place): - shape = [2, 2, 3, 3] - eps = 0.005 - 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, - num_filters=2, - filter_size=1, - padding=[1, 0, 1, 0], - bias_attr=False, - use_cudnn=True, - groups=1, - data_format="NHWC", - ) - x_arr = np.random.uniform(-1, 1, shape).astype(dtype) - - 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)) - gradient_checker.double_grad_check( - [x] + w, y, x_init=[x_arr] + w_arr, place=place, eps=eps - ) - @test_with_pir_api @prog_scope() def func_pir(self, place): @@ -512,35 +294,10 @@ def test_grad(self): if core.is_compiled_with_cuda(): places.append(base.CUDAPlace(0)) for p in places: - self.func(p) self.func_pir(p) class TestConv3DDoubleGradCheck_AsyPadding(unittest.TestCase): - @prog_scope() - def func(self, place): - shape = [2, 2, 2, 2, 2] - eps = 0.005 - 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, - num_filters=2, - filter_size=1, - padding=[1, 0, 0, 1, 1, 2], - bias_attr=False, - use_cudnn=True, - ) - x_arr = np.random.uniform(-1, 1, shape).astype(dtype) - - 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)) - gradient_checker.double_grad_check( - [x] + w, y, x_init=[x_arr] + w_arr, place=place, eps=eps - ) - @test_with_pir_api @prog_scope() def func_pir(self, place): @@ -564,36 +321,10 @@ def test_grad(self): if core.is_compiled_with_cuda(): places.append(base.CUDAPlace(0)) for p in places: - self.func(p) self.func_pir(p) class TestConv3DoubleGradCheck_PaddingSAME(unittest.TestCase): - @prog_scope() - def func(self, place): - shape = [2, 2, 2, 2, 2] - eps = 0.005 - 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, - num_filters=2, - filter_size=1, - padding="SAME", - groups=1, - bias_attr=False, - use_cudnn=True, - ) - x_arr = np.random.uniform(-1, 1, shape).astype(dtype) - - 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)) - gradient_checker.double_grad_check( - [x] + w, y, x_init=[x_arr] + w_arr, place=place, eps=eps - ) - @test_with_pir_api @prog_scope() def func_pir(self, place): @@ -617,35 +348,10 @@ def test_grad(self): if core.is_compiled_with_cuda(): places.append(base.CUDAPlace(0)) for p in places: - self.func(p) self.func_pir(p) class TestConv3DoubleGradCheck_PaddingVALID(unittest.TestCase): - @prog_scope() - def func(self, place): - shape = [2, 2, 3, 3, 2] - eps = 0.005 - 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, - num_filters=2, - filter_size=1, - padding="VALID", - bias_attr=False, - use_cudnn=True, - ) - x_arr = np.random.uniform(-1, 1, shape).astype(dtype) - - 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)) - gradient_checker.double_grad_check( - [x] + w, y, x_init=[x_arr] + w_arr, place=place, eps=eps - ) - @test_with_pir_api @prog_scope() def func_pir(self, place): @@ -669,37 +375,10 @@ def test_grad(self): if core.is_compiled_with_cuda(): places.append(base.CUDAPlace(0)) for p in places: - self.func(p) self.func_pir(p) class TestConv3DDoubleGradCheck_ChannelLast(unittest.TestCase): - @prog_scope() - def func(self, place): - shape = [2, 2, 2, 2, 3] - eps = 0.005 - 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, - num_filters=2, - filter_size=1, - padding=[1, 1, 1], - bias_attr=False, - use_cudnn=True, - groups=1, - data_format="NDHWC", - ) - x_arr = np.random.uniform(-1, 1, shape).astype(dtype) - - 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)) - gradient_checker.double_grad_check( - [x] + w, y, x_init=[x_arr] + w_arr, place=place, eps=eps - ) - @test_with_pir_api @prog_scope() def func_pir(self, place): @@ -724,37 +403,10 @@ def test_grad(self): if core.is_compiled_with_cuda(): places.append(base.CUDAPlace(0)) for p in places: - self.func(p) self.func_pir(p) class TestConv3DDoubleGradCheck_ChannelLast_AsyPadding(unittest.TestCase): - @prog_scope() - def func(self, place): - shape = [2, 2, 2, 2, 3] - eps = 0.005 - 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, - num_filters=2, - filter_size=1, - padding=[1, 0, 1, 0, 1, 0], - bias_attr=False, - use_cudnn=True, - groups=1, - data_format="NDHWC", - ) - x_arr = np.random.uniform(-1, 1, shape).astype(dtype) - - 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)) - gradient_checker.double_grad_check( - [x] + w, y, x_init=[x_arr] + w_arr, place=place, eps=eps - ) - @test_with_pir_api @prog_scope() def func_pir(self, place): @@ -779,35 +431,10 @@ def test_grad(self): if core.is_compiled_with_cuda(): places.append(base.CUDAPlace(0)) for p in places: - self.func(p) self.func_pir(p) class TestDepthWiseConvDoubleGradCheck(unittest.TestCase): - @prog_scope() - def func(self, place): - shape = [2, 4, 3, 3] - eps = 0.005 - dtype = np.float32 if base.core.is_compiled_with_rocm() else np.float64 - x = paddle.static.data('x', shape, dtype) - - # condition of depthwise conv: - # use_cudnn == False - # groups == filters - # num_filters % num_channels == 0 - y = paddle.static.nn.conv2d( - x, shape[1], 1, groups=shape[1], bias_attr=False, use_cudnn=False - ) - x_arr = np.random.uniform(-1, 1, shape).astype(dtype) - - 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)) - gradient_checker.double_grad_check( - [x] + w, y, x_init=[x_arr] + w_arr, place=place, eps=eps - ) - @test_with_pir_api @prog_scope() def func_pir(self, place): @@ -832,7 +459,6 @@ def test_grad(self): if core.is_compiled_with_cuda(): places.append(base.CUDAPlace(0)) for p in places: - self.func(p) self.func_pir(p) diff --git a/test/deprecated/legacy_test/test_data_norm_op.py b/test/legacy_test/test_data_norm_op.py similarity index 91% rename from test/deprecated/legacy_test/test_data_norm_op.py rename to test/legacy_test/test_data_norm_op.py index 954c3da834fd7b..8c6c4d599f180e 100644 --- a/test/deprecated/legacy_test/test_data_norm_op.py +++ b/test/legacy_test/test_data_norm_op.py @@ -19,9 +19,7 @@ from op import Operator from op_test import OpTest -import paddle -from paddle import base -from paddle.base import Program, core, program_guard +from paddle.base import core def _reference_testing(x, batch_size, batch_sum, batch_square_sum, slot_dim=-1): @@ -524,37 +522,5 @@ def test_check_grad(self): self.check_grad(['X'], 'Y', no_grad_set=set(), check_dygraph=False) -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, base.data_norm, x2) - paddle.static.nn.data_norm( - input=x2, param_attr={}, enable_scale_and_shift=True - ) - - # Test input with dimension 1 - paddle.enable_static() - x3 = paddle.static.data("", shape=[0], dtype="float32") - self.assertRaises(ValueError, paddle.static.nn.data_norm, x3) - - # The size of input in data_norm should not be 0. - 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 = 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, - feed={'x': np.ones([0, 3]).astype('float32')}, - ) - - self.assertRaises(ValueError, test_0_size) - - if __name__ == '__main__': unittest.main() diff --git a/test/legacy_test/test_deform_conv2d.py b/test/legacy_test/test_deform_conv2d.py new file mode 100644 index 00000000000000..95e180c373842d --- /dev/null +++ b/test/legacy_test/test_deform_conv2d.py @@ -0,0 +1,346 @@ +# Copyright (c) 2020 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 unittest import TestCase + +import numpy as np + +import paddle +import paddle.nn.initializer as I +from paddle.pir_utils import test_with_pir_api + + +class TestDeformConv2D(TestCase): + batch_size = 4 + spatial_shape = (5, 5) + dtype = "float32" + + def setUp(self): + self.in_channels = 2 + self.out_channels = 5 + self.kernel_size = [3, 3] + self.padding = [0, 0] + self.stride = [1, 1] + self.dilation = [1, 1] + self.deformable_groups = 1 + self.groups = 1 + self.no_bias = True + + def prepare(self): + np.random.seed(1) + paddle.seed(1) + if isinstance(self.kernel_size, int): + filter_shape = (self.kernel_size,) * 2 + else: + filter_shape = tuple(self.kernel_size) + self.filter_shape = filter_shape + + self.weight = np.random.uniform( + -1, + 1, + (self.out_channels, self.in_channels // self.groups) + filter_shape, + ).astype(self.dtype) + if not self.no_bias: + self.bias = np.random.uniform(-1, 1, (self.out_channels,)).astype( + self.dtype + ) + + def out_size( + in_size, pad_size, dilation_size, kernel_size, stride_size + ): + return ( + in_size + 2 * pad_size - (dilation_size * (kernel_size - 1) + 1) + ) / stride_size + 1 + + out_h = int( + out_size( + self.spatial_shape[0], + self.padding[0], + self.dilation[0], + self.kernel_size[0], + self.stride[0], + ) + ) + out_w = int( + out_size( + self.spatial_shape[1], + self.padding[1], + self.dilation[1], + self.kernel_size[1], + self.stride[1], + ) + ) + out_shape = (out_h, out_w) + + self.input_shape = ( + self.batch_size, + self.in_channels, + ) + self.spatial_shape + + self.offset_shape = ( + self.batch_size, + self.deformable_groups * 2 * filter_shape[0] * filter_shape[1], + ) + out_shape + + self.mask_shape = ( + self.batch_size, + self.deformable_groups * filter_shape[0] * filter_shape[1], + ) + out_shape + + self.input = np.random.uniform(-1, 1, self.input_shape).astype( + self.dtype + ) + + self.offset = np.random.uniform(-1, 1, self.offset_shape).astype( + self.dtype + ) + + self.mask = np.random.uniform(-1, 1, self.mask_shape).astype(self.dtype) + + def static_graph_case_dcn(self): + main = paddle.static.Program() + start = paddle.static.Program() + paddle.enable_static() + with paddle.static.program_guard(main, start): + x = paddle.static.data( + "input", (-1, self.in_channels, -1, -1), dtype=self.dtype + ) + offset = paddle.static.data( + "offset", + ( + -1, + self.deformable_groups + * 2 + * self.filter_shape[0] + * self.filter_shape[1], + -1, + -1, + ), + dtype=self.dtype, + ) + mask = paddle.static.data( + "mask", + ( + -1, + self.deformable_groups + * self.filter_shape[0] + * self.filter_shape[1], + -1, + -1, + ), + dtype=self.dtype, + ) + + y_v1 = paddle.vision.ops.DeformConv2D( + in_channels=self.in_channels, + out_channels=self.out_channels, + kernel_size=self.filter_shape, + stride=self.stride, + padding=self.padding, + dilation=self.dilation, + groups=self.groups, + deformable_groups=self.deformable_groups, + weight_attr=I.Assign(self.weight), + bias_attr=False if self.no_bias else I.Assign(self.bias), + )(x, offset, None) + + y_v2 = paddle.vision.ops.DeformConv2D( + in_channels=self.in_channels, + out_channels=self.out_channels, + kernel_size=self.filter_shape, + stride=self.stride, + padding=self.padding, + dilation=self.dilation, + groups=self.groups, + deformable_groups=self.deformable_groups, + weight_attr=I.Assign(self.weight), + bias_attr=False if self.no_bias else I.Assign(self.bias), + )(x, offset, mask) + + exe = paddle.static.Executor(self.place) + exe.run(start) + out_v1, out_v2 = exe.run( + main, + feed={ + "input": self.input, + "offset": self.offset, + "mask": self.mask, + }, + fetch_list=[y_v1, y_v2], + ) + return out_v1, out_v2 + + def dygraph_case_dcn(self): + paddle.disable_static() + x = paddle.to_tensor(self.input) + offset = paddle.to_tensor(self.offset) + mask = paddle.to_tensor(self.mask) + + bias = None if self.no_bias else paddle.to_tensor(self.bias) + + deform_conv2d = paddle.vision.ops.DeformConv2D( + in_channels=self.in_channels, + out_channels=self.out_channels, + kernel_size=self.kernel_size, + stride=self.stride, + padding=self.padding, + dilation=self.dilation, + deformable_groups=self.deformable_groups, + groups=self.groups, + weight_attr=I.Assign(self.weight), + bias_attr=False if self.no_bias else I.Assign(self.bias), + ) + + y_v1 = deform_conv2d(x, offset) + y_v2 = deform_conv2d(x, offset, mask) + + out_v1 = y_v1.numpy() + out_v2 = y_v2.numpy() + + return out_v1, out_v2 + + @test_with_pir_api + def _test_identity(self): + self.prepare() + static_dcn_v1, static_dcn_v2 = self.static_graph_case_dcn() + dy_dcn_v1, dy_dcn_v2 = self.dygraph_case_dcn() + np.testing.assert_array_almost_equal(static_dcn_v1, dy_dcn_v1) + np.testing.assert_array_almost_equal(static_dcn_v2, dy_dcn_v2) + + def test_identity(self): + self.place = paddle.CPUPlace() + self._test_identity() + + if paddle.is_compiled_with_cuda(): + self.place = paddle.CUDAPlace(0) + self._test_identity() + + +# testcases for DeformConv2D +class TestDeformConv2DWithPadding(TestDeformConv2D): + def setUp(self): + self.in_channels = 3 + self.out_channels = 5 + self.kernel_size = [3, 3] + self.padding = [2, 2] + self.stride = [1, 1] + self.dilation = [1, 1] + self.deformable_groups = 1 + self.groups = 1 + self.no_bias = True + + +class TestDeformConv2DWithBias(TestDeformConv2D): + def setUp(self): + self.in_channels = 3 + self.out_channels = 5 + self.kernel_size = [3, 3] + self.padding = [2, 2] + self.stride = [1, 1] + self.dilation = [1, 1] + self.deformable_groups = 1 + self.groups = 1 + self.no_bias = False + + +class TestDeformConv2DWithAsynPadding(TestDeformConv2D): + def setUp(self): + self.in_channels = 3 + self.out_channels = 5 + self.kernel_size = [3, 3] + self.padding = [1, 2] + self.stride = [1, 1] + self.dilation = [1, 1] + self.deformable_groups = 1 + self.groups = 1 + self.no_bias = False + + +class TestDeformConv2DWithDilation(TestDeformConv2D): + def setUp(self): + self.in_channels = 3 + self.out_channels = 5 + self.kernel_size = [3, 3] + self.padding = [1, 1] + self.stride = [1, 1] + self.dilation = [3, 3] + self.deformable_groups = 1 + self.groups = 1 + self.no_bias = False + + +class TestDeformConv2DWithStride(TestDeformConv2D): + def setUp(self): + self.in_channels = 3 + self.out_channels = 5 + self.kernel_size = [3, 3] + self.padding = [1, 1] + self.stride = [2, 2] + self.dilation = [1, 1] + self.deformable_groups = 1 + self.groups = 1 + self.no_bias = False + + +class TestDeformConv2DWithDeformable_Groups(TestDeformConv2D): + def setUp(self): + self.in_channels = 5 + self.out_channels = 5 + self.kernel_size = [3, 3] + self.padding = [1, 1] + self.stride = [1, 1] + self.dilation = [1, 1] + self.deformable_groups = 5 + self.groups = 1 + self.no_bias = False + + +class TestDeformConv2DWithGroups(TestDeformConv2D): + def setUp(self): + self.in_channels = 5 + self.out_channels = 5 + self.kernel_size = [3, 3] + self.padding = [1, 1] + self.stride = [1, 1] + self.dilation = [1, 1] + self.deformable_groups = 1 + self.groups = 5 + self.no_bias = False + + +class TestDeformConv2DError(unittest.TestCase): + @test_with_pir_api + def test_input_error(self): + def test_input_rank_error(): + paddle.enable_static() + x = paddle.static.data(name='error_x_1', shape=[0], dtype='float32') + offset = paddle.static.data( + name='error_offset_1', shape=[0], dtype='float32' + ) + mask = paddle.static.data( + name='error_mask_1', shape=[0, 0, 0], dtype='float32' + ) + out = paddle.vision.ops.DeformConv2D( + in_channels=0, + out_channels=0, + kernel_size=0, + deformable_groups=0, + )(x, offset, mask) + + self.assertRaises(AssertionError, test_input_rank_error) + + +if __name__ == "__main__": + unittest.main() diff --git a/test/deprecated/legacy_test/test_deformable_conv_op.py b/test/legacy_test/test_deformable_conv_op.py similarity index 85% rename from test/deprecated/legacy_test/test_deformable_conv_op.py rename to test/legacy_test/test_deformable_conv_op.py index 63d939cc856267..23b49f4f936063 100644 --- a/test/deprecated/legacy_test/test_deformable_conv_op.py +++ b/test/legacy_test/test_deformable_conv_op.py @@ -372,73 +372,6 @@ def init_test_case(self): class TestModulatedDeformableConvInvalidInput(unittest.TestCase): - def test_error(self): - def test_invalid_input(): - paddle.enable_static() - input = [1, 3, 32, 32] - offset = paddle.static.data( - name='offset', shape=[None, 3, 32, 32], dtype='float32' - ) - mask = paddle.static.data( - name='mask', shape=[None, 3, 32, 32], dtype='float32' - ) - loss = paddle.static.nn.common.deformable_conv( - input, offset, mask, num_filters=4, filter_size=1 - ) - - self.assertRaises(TypeError, test_invalid_input) - - def test_invalid_offset(): - paddle.enable_static() - input = paddle.static.data( - name='input', shape=[None, 3, 32, 32], dtype='int32' - ) - offset = paddle.static.data( - name='offset', shape=[None, 3, 32, 32], dtype='float32' - ) - mask = paddle.static.data( - name='mask', shape=[None, 3, 32, 32], dtype='float32' - ) - loss = paddle.static.nn.common.deformable_conv( - input, offset, mask, num_filters=4, filter_size=1 - ) - - self.assertRaises(TypeError, test_invalid_offset) - - def test_invalid_filter(): - paddle.enable_static() - input = paddle.static.data( - name='input_filter', shape=[None, 3, 32, 32], dtype='float32' - ) - offset = paddle.static.data( - name='offset_filter', shape=[None, 3, 32, 32], dtype='float32' - ) - mask = paddle.static.data( - name='mask_filter', shape=[None, 3, 32, 32], dtype='float32' - ) - loss = paddle.static.nn.common.deformable_conv( - input, offset, mask, num_filters=4, filter_size=0 - ) - - self.assertRaises(ValueError, test_invalid_filter) - - def test_invalid_groups(): - paddle.enable_static() - input = paddle.static.data( - name='input_groups', shape=[1, 1, 1, 1], dtype='float32' - ) - offset = paddle.static.data( - name='offset_groups', shape=[1, 1], dtype='float32' - ) - mask = paddle.static.data( - name='mask_groups', shape=[1], dtype='float32' - ) - paddle.static.nn.deform_conv2d( - input, offset, mask, 1, 1, padding=1, groups=0 - ) - - self.assertRaises(ValueError, test_invalid_groups) - @test_with_pir_api def test_error_api(self): def test_invalid_input(): diff --git a/test/legacy_test/test_device_guard.py b/test/legacy_test/test_device_guard.py new file mode 100644 index 00000000000000..9d53982992ab7f --- /dev/null +++ b/test/legacy_test/test_device_guard.py @@ -0,0 +1,106 @@ +# Copyright (c) 2020 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 + +paddle.enable_static() + + +def execute(main_program, startup_program): + if paddle.is_compiled_with_cuda(): + place = paddle.CUDAPlace(0) + else: + place = paddle.CPUPlace() + exe = paddle.static.Executor(place) + exe.run(startup_program) + exe.run(main_program) + + +def get_valid_warning_num(warning, w): + num = 0 + for i in range(len(w)): + if warning in str(w[i].message): + num += 1 + return num + + +class TestDeviceGuard(unittest.TestCase): + def test_cpu_only_op(self): + main_program = paddle.static.Program() + startup_program = paddle.static.Program() + with paddle.static.program_guard(main_program, startup_program): + x = paddle.full( + shape=[2, 255, 13, 13], fill_value=0.3, dtype='float32' + ) + gt_box = paddle.full( + shape=[2, 6, 4], fill_value=0.5, dtype='float32' + ) + gt_label = paddle.full(shape=[2, 6], fill_value=1.0, dtype='int32') + gt_score = paddle.full( + shape=[2, 6], fill_value=0.5, dtype='float32' + ) + anchors = [ + 10, + 13, + 16, + 30, + 33, + 23, + 30, + 61, + 62, + 45, + 59, + 119, + 116, + 90, + 156, + 198, + 373, + 326, + ] + anchor_mask = [0, 1, 2] + with paddle.static.device_guard("gpu"): + # yolo_loss only has cpu kernel, so its cpu kernel will be executed + loss = paddle.vision.ops.yolo_loss( + x=x, + gt_box=gt_box, + gt_label=gt_label, + gt_score=gt_score, + anchors=anchors, + anchor_mask=anchor_mask, + class_num=80, + ignore_thresh=0.7, + downsample_ratio=32, + ) + + execute(main_program, startup_program) + + def test_error(self): + def device_attr(): + with paddle.static.device_guard("cpu1"): + out = paddle.full(shape=[1], fill_value=0.2, dtype='float32') + + def device_attr2(): + with paddle.static.device_guard("cpu:1"): + out = paddle.full(shape=[1], fill_value=0.2, dtype='float32') + + self.assertRaises(ValueError, device_attr) + self.assertRaises(ValueError, device_attr2) + + +if __name__ == '__main__': + unittest.main() diff --git a/test/deprecated/legacy_test/test_executor_and_use_program_cache.py b/test/legacy_test/test_executor_and_use_program_cache.py similarity index 66% rename from test/deprecated/legacy_test/test_executor_and_use_program_cache.py rename to test/legacy_test/test_executor_and_use_program_cache.py index c43b58f027ca15..15df67914856d0 100644 --- a/test/deprecated/legacy_test/test_executor_and_use_program_cache.py +++ b/test/legacy_test/test_executor_and_use_program_cache.py @@ -25,66 +25,6 @@ from paddle.pir_utils import test_with_pir_api -class TestExecutor(unittest.TestCase): - def test_mul(self): - 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) - b.desc.set_need_check_feed(False) - output = paddle.matmul(x=a, y=b) - - # Compute with numpy - a_np = np.random.random((100, 784)).astype('float32') - b_np = np.random.random((784, 100)).astype('float32') - out_np = np.dot(a_np, b_np) - - place = paddle.CPUPlace() - exe = base.Executor(place) - - def _train(use_program_cache, max_iters=1): - import time - - run_time = 0.0 - for i in range(max_iters): - begin = time.time() - outs = exe.run( - program=main_program, - feed={'a': a_np, 'b': b_np}, - fetch_list=[output], - use_program_cache=use_program_cache, - ) - end = time.time() - run_time += end - begin - out = outs[0] - self.assertEqual((100, 100), out.shape) - np.testing.assert_allclose(out, out_np, rtol=1e-05) - return run_time - - max_iters = 3 - run_time_with_cache = _train( - use_program_cache=True, max_iters=max_iters - ) - print("run time with program cache: %f" % run_time_with_cache) - - run_time_without_cache = _train( - use_program_cache=False, max_iters=max_iters - ) - print("run time without program cache: %f" % run_time_without_cache) - - run_time_with_cache = _train( - use_program_cache=True, max_iters=max_iters - ) - print("run time with program cache: %f" % run_time_with_cache) - - run_time_with_cache = _train( - use_program_cache=True, max_iters=max_iters - ) - print("run time with program cache: %f" % run_time_with_cache) - - class ExecutorPaddingRNNTest(PaddingRNNTestBase): def train_and_save_inference_program( self, rnn_model="static", use_program_cache=True diff --git a/test/legacy_test/test_fc_op.py b/test/legacy_test/test_fc_op.py new file mode 100644 index 00000000000000..d61c93361097b7 --- /dev/null +++ b/test/legacy_test/test_fc_op.py @@ -0,0 +1,136 @@ +# Copyright (c) 2018 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 op_test import OpTest + +SEED = 2020 + + +def fc_refer(matrix, with_bias, with_relu=False): + in_n, in_c, in_h, in_w = matrix.input.shape + w_i, w_o = matrix.weights.shape + + x_data = np.reshape(matrix.input, [in_n, in_c * in_h * in_w]) + w_data = np.reshape(matrix.weights, [w_i, w_o]) + b_data = np.reshape(matrix.bias, [1, w_o]) + result = None + + if with_bias: + result = np.dot(x_data, w_data) + b_data + else: + result = np.dot(x_data, w_data) + + if with_relu: + return np.maximum(result, 0) + else: + return result + + +class MatrixGenerate: + def __init__(self, mb, ic, oc, h, w, bias_dims=2): + self.input = np.random.random((mb, ic, h, w)).astype("float32") + self.weights = np.random.random((ic * h * w, oc)).astype("float32") + if bias_dims == 2: + self.bias = np.random.random((1, oc)).astype("float32") + else: + self.bias = np.random.random(oc).astype("float32") + + +class TestFCOp(OpTest): + def config(self): + self.with_bias = True + self.with_relu = True + self.matrix = MatrixGenerate(1, 10, 15, 3, 3, 2) + + def setUp(self): + self.op_type = "fc" + self.config() + + if self.with_bias: + self.inputs = { + 'Input': self.matrix.input, + 'W': self.matrix.weights, + 'Bias': self.matrix.bias, + } + else: + self.inputs = {'Input': self.matrix.input, 'W': self.matrix.weights} + + if self.with_relu: + activation_type = "relu" + else: + activation_type = "" + self.attrs = {'use_mkldnn': False, 'activation_type': activation_type} + + self.outputs = { + 'Out': fc_refer(self.matrix, self.with_bias, self.with_relu) + } + + def test_check_output(self): + self.check_output(check_dygraph=False) + + +class TestFCOpNoBias1(TestFCOp): + def config(self): + self.with_bias = False + self.with_relu = False + self.matrix = MatrixGenerate(2, 8, 10, 1, 1, 2) + + +class TestFCOpNoBias2(TestFCOp): + def config(self): + self.with_bias = False + self.with_relu = False + self.matrix = MatrixGenerate(4, 5, 6, 2, 2, 1) + + +class TestFCOpNoBias4(TestFCOp): + def config(self): + self.with_bias = False + self.with_relu = False + self.matrix = MatrixGenerate(1, 32, 64, 3, 3, 1) + + +class TestFCOpWithBias1(TestFCOp): + def config(self): + self.with_bias = True + self.with_relu = False + self.matrix = MatrixGenerate(3, 8, 10, 2, 1, 2) + + +class TestFCOpWithBias2(TestFCOp): + def config(self): + self.with_bias = True + self.with_relu = True + self.matrix = MatrixGenerate(4, 5, 6, 2, 2, 1) + + +class TestFCOpWithBias3(TestFCOp): + def config(self): + self.with_bias = True + self.with_relu = True + self.matrix = MatrixGenerate(1, 64, 32, 3, 3, 1) + + +class TestFCOpWithPadding(TestFCOp): + def config(self): + self.with_bias = True + self.with_relu = True + self.matrix = MatrixGenerate(1, 4, 3, 128, 128, 2) + + +if __name__ == "__main__": + unittest.main() diff --git a/test/legacy_test/test_functional_conv2d.py b/test/legacy_test/test_functional_conv2d.py new file mode 100644 index 00000000000000..2f7d18e29566f9 --- /dev/null +++ b/test/legacy_test/test_functional_conv2d.py @@ -0,0 +1,284 @@ +# Copyright (c) 2020 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 unittest import TestCase + +import numpy as np + +import paddle +import paddle.base.dygraph as dg +import paddle.nn.functional as F +from paddle import base + + +class TestFunctionalConv2DError(TestCase): + batch_size = 4 + spatial_shape = (16, 16) + dtype = "float32" + + def setUp(self): + self.in_channels = 3 + self.out_channels = 5 + self.filter_shape = 3 + self.padding = "not_valid" + self.stride = 1 + self.dilation = 1 + self.groups = 1 + self.no_bias = False + self.act = "sigmoid" + self.data_format = "NHWC" + + def test_exception(self): + self.prepare() + with self.assertRaises(ValueError): + self.static_graph_case() + + def prepare(self): + if isinstance(self.filter_shape, int): + filter_shape = (self.filter_shape,) * 2 + else: + filter_shape = tuple(self.filter_shape) + self.weight_shape = ( + self.out_channels, + self.in_channels // self.groups, + ) + filter_shape + self.bias_shape = (self.out_channels,) + + def static_graph_case(self): + 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( + "input", + (-1, -1, -1, self.in_channels), + dtype=self.dtype, + ) + else: + x = paddle.static.data( + "input", + (-1, self.in_channels, -1, -1), + dtype=self.dtype, + ) + weight = paddle.static.data( + "weight", self.weight_shape, dtype=self.dtype + ) + if not self.no_bias: + bias = paddle.static.data( + "bias", self.bias_shape, dtype=self.dtype + ) + y = F.conv2d( + x, + weight, + None if self.no_bias else bias, + padding=self.padding, + stride=self.stride, + dilation=self.dilation, + groups=self.groups, + data_format=self.data_format, + ) + + +class TestFunctionalConv2DErrorCase2(TestFunctionalConv2DError): + def setUp(self): + self.in_channels = 3 + self.out_channels = 5 + self.filter_shape = 3 + self.padding = [[0, 0], [1, 2], [3, 4], [5, 6]] + self.stride = 1 + self.dilation = 1 + self.groups = 1 + self.no_bias = False + self.act = "sigmoid" + self.use_cudnn = False + self.data_format = "NCHW" + + +class TestFunctionalConv2DErrorCase3(TestFunctionalConv2DError): + def setUp(self): + self.in_channels = 3 + self.out_channels = 4 + self.filter_shape = 3 + self.padding = "same" + self.stride = 1 + self.dilation = 1 + self.groups = 2 + self.no_bias = False + self.act = "sigmoid" + self.use_cudnn = False + self.data_format = "not_valid" + + +class TestFunctionalConv2DErrorCase4(TestFunctionalConv2DError): + def setUp(self): + self.in_channels = 4 + self.out_channels = 3 + self.filter_shape = 3 + self.padding = "same" + self.stride = 1 + self.dilation = 1 + self.groups = 2 + self.no_bias = False + self.act = "sigmoid" + self.use_cudnn = False + self.data_format = "NCHW" + + +class TestFunctionalConv2DErrorCase7(TestFunctionalConv2DError): + def setUp(self): + self.in_channels = 3 + self.out_channels = 5 + self.filter_shape = 3 + self.padding = "same" + self.stride = 1 + self.dilation = 1 + self.groups = 1 + self.no_bias = False + self.act = "sigmoid" + self.use_cudnn = True + self.data_format = "not_valid" + + +class TestFunctionalConv2DErrorCase8(TestFunctionalConv2DError): + def setUp(self): + self.in_channels = 3 + self.out_channels = 5 + self.filter_shape = 3 + self.padding = [1, 2, 1, 2, 1] + self.stride = 1 + self.dilation = 1 + self.groups = 1 + self.no_bias = False + self.act = "sigmoid" + self.use_cudnn = True + self.data_format = "NCHW" + + +class TestFunctionalConv2DErrorCase9(TestFunctionalConv2DError): + def setUp(self): + self.in_channels = -5 + self.out_channels = 5 + self.filter_shape = 3 + self.padding = [[0, 0], [0, 0], [3, 2], [1, 2]] + self.stride = 1 + self.dilation = 1 + self.groups = 1 + self.no_bias = False + self.act = "sigmoid" + self.use_cudnn = False + self.data_format = "NCHW" + + +class TestFunctionalConv2DErrorCase10(TestFunctionalConv2DError): + def setUp(self): + self.in_channels = 3 + self.out_channels = 4 + self.filter_shape = 3 + self.padding = "same" + self.stride = 1 + self.dilation = 1 + self.groups = 2 + self.no_bias = False + self.act = "sigmoid" + self.use_cudnn = False + self.data_format = "NHWC" + + +class TestFunctionalConv2DErrorCase11(TestFunctionalConv2DError): + def setUp(self): + self.in_channels = 3 + self.out_channels = 5 + self.filter_shape = 3 + self.padding = 0 + self.stride = 1 + self.dilation = 1 + self.groups = 1 + self.no_bias = False + self.act = "sigmoid" + self.use_cudnn = False + self.data_format = "NHCW" + + +class TestFunctionalConv2DErrorCase12(TestCase): + def setUp(self): + self.input = np.array([]) + self.filter = np.array([]) + self.num_filters = 0 + self.filter_size = 0 + self.bias = None + self.padding = 0 + self.stride = 1 + self.dilation = 1 + self.groups = 1 + self.data_format = "NCHW" + + def dygraph_case(self): + with dg.guard(): + x = paddle.to_tensor(self.input, dtype=paddle.float32) + w = paddle.to_tensor(self.filter, dtype=paddle.float32) + b = ( + None + if self.bias is None + else paddle.to_tensor(self.bias, dtype=paddle.float32) + ) + y = F.conv2d( + x, + w, + b, + padding=self.padding, + stride=self.stride, + dilation=self.dilation, + groups=self.groups, + data_format=self.data_format, + ) + + def test_dygraph_exception(self): + with self.assertRaises(ValueError): + self.dygraph_case() + + +class TestFunctionalConv2DErrorCase13(TestFunctionalConv2DErrorCase12): + def setUp(self): + self.input = np.random.randn(1, 3, 3, 3) + self.filter = np.random.randn(3, 3, 1, 1) + self.num_filters = 3 + self.filter_size = 1 + self.bias = None + self.padding = 0 + self.stride = 1 + self.dilation = 1 + self.groups = 0 + self.data_format = "NCHW" + + +class TestFunctionalConv2DErrorCase14(TestFunctionalConv2DErrorCase12): + def setUp(self): + self.input = np.random.randn(0, 0, 0, 0) + self.filter = np.random.randn(1, 0, 0, 0) + self.num_filters = 0 + self.filter_size = 0 + self.bias = None + self.padding = 0 + self.stride = 1 + self.dilation = 1 + self.groups = 1 + self.data_format = "NCHW" + + +if __name__ == "__main__": + paddle.enable_static() + unittest.main() diff --git a/test/legacy_test/test_functional_conv3d.py b/test/legacy_test/test_functional_conv3d.py new file mode 100644 index 00000000000000..bdfd2c7e6116fd --- /dev/null +++ b/test/legacy_test/test_functional_conv3d.py @@ -0,0 +1,265 @@ +# Copyright (c) 2020 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 unittest import TestCase + +import numpy as np + +import paddle +import paddle.base.dygraph as dg +import paddle.nn.functional as F +from paddle import base + + +class TestFunctionalConv3DError(TestCase): + batch_size = 4 + spatial_shape = (8, 8, 8) + dtype = "float32" + + def setUp(self): + self.in_channels = 3 + self.out_channels = 5 + self.filter_shape = 3 + self.padding = "not_valid" + self.stride = 1 + self.dilation = 1 + self.groups = 1 + self.no_bias = False + self.act = "sigmoid" + self.data_format = "NDHWC" + + def test_exception(self): + self.prepare() + with self.assertRaises(ValueError): + self.static_graph_case() + + def prepare(self): + if isinstance(self.filter_shape, int): + filter_shape = (self.filter_shape,) * 3 + else: + filter_shape = tuple(self.filter_shape) + self.weight_shape = ( + self.out_channels, + self.in_channels // self.groups, + ) + filter_shape + self.bias_shape = (self.out_channels,) + + def static_graph_case(self): + 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( + "input", + (-1, -1, -1, -1, self.in_channels), + dtype=self.dtype, + ) + else: + x = paddle.static.data( + "input", + (-1, self.in_channels, -1, -1, -1), + dtype=self.dtype, + ) + weight = paddle.static.data( + "weight", self.weight_shape, dtype=self.dtype + ) + if not self.no_bias: + bias = paddle.static.data( + "bias", self.bias_shape, dtype=self.dtype + ) + y = F.conv3d( + x, + weight, + None if self.no_bias else bias, + padding=self.padding, + stride=self.stride, + dilation=self.dilation, + groups=self.groups, + data_format=self.data_format, + ) + + if self.act == 'sigmoid': + y = F.sigmoid(y) + + +class TestFunctionalConv3DErrorCase2(TestFunctionalConv3DError): + def setUp(self): + self.in_channels = 3 + self.out_channels = 5 + self.filter_shape = 3 + self.padding = [[0, 0], [1, 1], [1, 2], [3, 4], [5, 6]] + self.stride = 1 + self.dilation = 1 + self.groups = 1 + self.no_bias = False + self.act = "sigmoid" + self.data_format = "NCDHW" + + +class TestFunctionalConv3DErrorCase3(TestFunctionalConv3DError): + def setUp(self): + self.in_channels = 3 + self.out_channels = 4 + self.filter_shape = 3 + self.padding = "same" + self.stride = 1 + self.dilation = 1 + self.groups = 2 + self.no_bias = False + self.act = "sigmoid" + self.data_format = "not_valid" + + +class TestFunctionalConv3DErrorCase4(TestFunctionalConv3DError): + def setUp(self): + self.in_channels = 4 + self.out_channels = 3 + self.filter_shape = 3 + self.padding = "same" + self.stride = 1 + self.dilation = 1 + self.groups = 2 + self.no_bias = False + self.act = "sigmoid" + self.data_format = "NCDHW" + + +class TestFunctionalConv3DErrorCase7(TestFunctionalConv3DError): + def setUp(self): + self.in_channels = 3 + self.out_channels = 5 + self.filter_shape = 3 + self.padding = "same" + self.stride = 1 + self.dilation = 1 + self.groups = 1 + self.no_bias = False + self.act = "sigmoid" + self.data_format = "not_valid" + + +class TestFunctionalConv3DErrorCase8(TestFunctionalConv3DError): + def setUp(self): + self.in_channels = 3 + self.out_channels = 5 + self.filter_shape = 3 + self.padding = [1, 2, 1, 2, 1] + self.stride = 1 + self.dilation = 1 + self.groups = 1 + self.no_bias = False + self.act = "sigmoid" + self.data_format = "NCDHW" + + +class TestFunctionalConv3DErrorCase9(TestFunctionalConv3DError): + def setUp(self): + self.in_channels = -5 + self.out_channels = 5 + self.filter_shape = 3 + self.padding = [[0, 0], [0, 0], [3, 2], [1, 2], [1, 1]] + self.stride = 1 + self.dilation = 1 + self.groups = 1 + self.no_bias = False + self.act = "sigmoid" + self.data_format = "NCDHW" + + +class TestFunctionalConv3DErrorCase10(TestFunctionalConv3DError): + def setUp(self): + self.in_channels = 3 + self.out_channels = 4 + self.filter_shape = 3 + self.padding = "same" + self.stride = 1 + self.dilation = 1 + self.groups = 2 + self.no_bias = False + self.act = "sigmoid" + self.data_format = "NDHWC" + + +class TestFunctionalConv3DErrorCase11(TestCase): + def setUp(self): + self.input = np.array([]) + self.filter = np.array([]) + self.num_filters = 0 + self.filter_size = 0 + self.bias = None + self.padding = 0 + self.stride = 1 + self.dilation = 1 + self.groups = 1 + self.data_format = "NCDHW" + + def dygraph_case(self): + with dg.guard(): + x = paddle.to_tensor(self.input, dtype=paddle.float32) + w = paddle.to_tensor(self.filter, dtype=paddle.float32) + b = ( + None + if self.bias is None + else paddle.to_tensor(self.bias, dtype=paddle.float32) + ) + y = F.conv3d( + x, + w, + b, + padding=self.padding, + stride=self.stride, + dilation=self.dilation, + groups=self.groups, + data_format=self.data_format, + ) + + def test_dygraph_exception(self): + with self.assertRaises(ValueError): + self.dygraph_case() + + +class TestFunctionalConv3DErrorCase12(TestFunctionalConv3DErrorCase11): + def setUp(self): + self.input = np.random.randn(1, 3, 3, 3, 3) + self.filter = np.random.randn(3, 3, 1, 1, 1) + self.num_filters = 3 + self.filter_size = 1 + self.bias = None + self.padding = 0 + self.stride = 1 + self.dilation = 1 + self.groups = 0 + self.data_format = "NCDHW" + + +class TestFunctionalConv3DErrorCase13(TestFunctionalConv3DErrorCase11): + def setUp(self): + self.input = np.random.randn(0, 0, 0, 0, 0) + self.filter = np.random.randn(1, 0, 0, 0, 0) + self.num_filters = 1 + self.filter_size = 1 + self.bias = None + self.padding = 0 + self.stride = 1 + self.dilation = 1 + self.groups = 1 + self.data_format = "NCDHW" + + +if __name__ == "__main__": + paddle.enable_static() + unittest.main() diff --git a/test/deprecated/legacy_test/test_hsigmoid_op.py b/test/legacy_test/test_hsigmoid_op.py similarity index 89% rename from test/deprecated/legacy_test/test_hsigmoid_op.py rename to test/legacy_test/test_hsigmoid_op.py index ad3ae81821cdf3..f481fc2ebee2fa 100644 --- a/test/deprecated/legacy_test/test_hsigmoid_op.py +++ b/test/legacy_test/test_hsigmoid_op.py @@ -20,7 +20,6 @@ import paddle import paddle.nn.functional as F -from paddle import base from paddle.pir_utils import test_with_pir_api paddle.enable_static() @@ -283,91 +282,6 @@ def test_check_output(self): self.check_output(check_pir=True) -class TestHSigmoidOpWithSparseGrad(unittest.TestCase): - def hs_net_conf(self, is_sparse): - input_word = paddle.static.data(name="x", shape=[-1, 1], dtype='int64') - path_table = paddle.static.data( - name='path_table', shape=[-1, 3], dtype='int64' - ) - path_code = paddle.static.data( - name='path_code', shape=[-1, 3], dtype='int64' - ) - label = paddle.static.data(name='label', shape=[-1, 1], dtype='int64') - - data_list = [input_word, path_table, path_code, label] - - emb = paddle.static.nn.embedding( - input=input_word, - is_sparse=is_sparse, - size=[3, 3], - param_attr=base.ParamAttr( - initializer=paddle.nn.initializer.Normal(std=1 / math.sqrt(3)) - ), - ) - - loss = paddle.nn.HSigmoidLoss( - feature_size=emb.shape[1], - num_classes=3, - bias_attr=True, - is_custom=True, - is_sparse=is_sparse, - ) - - cost = loss( - input=emb, - label=label, - path_table=path_table, - path_code=path_code, - ) - - avg_cost = paddle.mean(cost) - - return avg_cost, data_list - - def training_test(self, is_sparse): - with paddle.static.program_guard( - paddle.static.Program(), paddle.static.Program() - ): - paddle.seed(1) - start_up = paddle.static.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') - label = np.array([1, 4]).astype('int64') - - loss, data_list = self.hs_net_conf(is_sparse) - optimizer = paddle.optimizer.SGD(learning_rate=1e-3) - optimizer.minimize(loss) - - main_program = paddle.static.default_main_program() - place = base.CPUPlace() - feeder = base.DataFeeder(feed_list=data_list, place=place) - exe = paddle.static.Executor(place) - - exe.run(start_up) - result = [] - for i in range(10): - data = [ - ( - [[x[i % 2]]], - [list(path_table[i % 2])], - [list(path_code[i % 2])], - [label[i % 2]], - ) - ] - - loss_val = exe.run( - main_program, feed=feeder.feed(data), fetch_list=[loss] - ) - result.append(loss_val) - return result - - def test_hs_grad_with_sparse(self): - dense_result = self.training_test(is_sparse=False) - sparse_result = self.training_test(is_sparse=True) - assert dense_result == sparse_result - - @skip_check_grad_ci( reason="[skip shape check] The huffman tree is structed separately. It will be complicated if use large shape." ) diff --git a/test/legacy_test/test_inference_api.py b/test/legacy_test/test_inference_api.py new file mode 100644 index 00000000000000..2bbf3ceb24431e --- /dev/null +++ b/test/legacy_test/test_inference_api.py @@ -0,0 +1,69 @@ +# 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. + +import unittest + +import paddle + +paddle.enable_static() +import numpy as np + +from paddle.base.core import PaddleDType, PaddleTensor + + +class TestInferenceApi(unittest.TestCase): + def test_inference_api(self): + tensor32 = np.random.randint(10, 20, size=[20, 2]).astype('int32') + paddletensor32 = PaddleTensor(tensor32) + dtype32 = paddletensor32.dtype + self.assertEqual(dtype32, PaddleDType.INT32) + self.assertEqual( + paddletensor32.data.tolist('int32'), tensor32.ravel().tolist() + ) + paddletensor32.data.reset(tensor32) + self.assertEqual( + paddletensor32.as_ndarray().ravel().tolist(), + tensor32.ravel().tolist(), + ) + + tensor64 = np.random.randint(10, 20, size=[20, 2]).astype('int64') + paddletensor64 = PaddleTensor(tensor64) + dtype64 = paddletensor64.dtype + self.assertEqual(dtype64, PaddleDType.INT64) + self.assertEqual( + paddletensor64.data.tolist('int64'), tensor64.ravel().tolist() + ) + paddletensor64.data.reset(tensor64) + self.assertEqual( + paddletensor64.as_ndarray().ravel().tolist(), + tensor64.ravel().tolist(), + ) + + tensor_float = np.random.randn(20, 2).astype('float32') + paddletensor_float = PaddleTensor(tensor_float) + dtype_float = paddletensor_float.dtype + self.assertEqual(dtype_float, PaddleDType.FLOAT32) + self.assertEqual( + paddletensor_float.data.tolist('float32'), + tensor_float.ravel().tolist(), + ) + paddletensor_float.data.reset(tensor_float) + self.assertEqual( + paddletensor_float.as_ndarray().ravel().tolist(), + tensor_float.ravel().tolist(), + ) + + +if __name__ == '__main__': + unittest.main() diff --git a/test/legacy_test/test_inference_model_io.py b/test/legacy_test/test_inference_model_io.py new file mode 100644 index 00000000000000..6b28a41bc4b085 --- /dev/null +++ b/test/legacy_test/test_inference_model_io.py @@ -0,0 +1,45 @@ +# Copyright (c) 2018 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.base import core, executor +from paddle.distributed.io import ( + load_inference_model_distributed, +) +from paddle.pir_utils import test_with_pir_api +from paddle.static.io import load_inference_model + +paddle.enable_static() + + +class TestLoadInferenceModelError(unittest.TestCase): + @test_with_pir_api + def test_load_model_not_exist(self): + place = core.CPUPlace() + exe = executor.Executor(place) + self.assertRaises( + ValueError, load_inference_model, './test_not_exist_dir/model', exe + ) + self.assertRaises( + ValueError, + load_inference_model_distributed, + './test_not_exist_dir', + exe, + ) + + +if __name__ == '__main__': + unittest.main() diff --git a/test/deprecated/legacy_test/test_layer_norm_op.py b/test/legacy_test/test_layer_norm_op.py similarity index 73% rename from test/deprecated/legacy_test/test_layer_norm_op.py rename to test/legacy_test/test_layer_norm_op.py index 29e129781bfe00..2fd1eb2b1a7470 100644 --- a/test/deprecated/legacy_test/test_layer_norm_op.py +++ b/test/legacy_test/test_layer_norm_op.py @@ -21,7 +21,6 @@ import paddle import paddle.nn.functional as F -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 @@ -580,268 +579,6 @@ def initConfig(self): self.check_pir = True -class TestLayerNormOp(unittest.TestCase): - def setUp(self): - self.use_cudnn = True - paddle.enable_static() - - def __assert_close(self, tensor, np_array, msg, atol=1e-4): - np.testing.assert_allclose( - np.array(tensor).flatten(), - np_array.flatten(), - rtol=1e-3, - atol=atol, - err_msg=msg, - ) - - def check_forward_backward( - self, - shape, - begin_norm_axis, - has_scale=True, - has_bias=True, - y_grad_scale=1.0, - use_mkldnn=False, - ): - def test_with_place( - place, shape, begin_norm_axis, use_mkldnn=use_mkldnn - ): - # attr - epsilon = 0.00001 - x_shape = shape - D = reduce(mul, x_shape[begin_norm_axis : len(x_shape)], 1) - scale_shape = [D] - - np.random.seed(123) - x = np.random.random_sample(x_shape).astype(np.float32) - scale = ( - np.random.random_sample(scale_shape).astype(np.float32) - if has_scale - else None - ) - bias = ( - np.random.random_sample(scale_shape).astype(np.float32) - if has_bias - else None - ) - y_grad = (np.random.random_sample(x_shape) * y_grad_scale).astype( - np.float32 - ) - - # reference forward & backward - y, mean, variance = _reference_layer_norm_naive( - x, scale, bias, epsilon, begin_norm_axis - ) - x_grad, scale_grad, bias_grad = _reference_layer_norm_grad( - x, y_grad, scale, bias, mean, variance, begin_norm_axis - ) - - var_dict = locals() - var_dict['y@GRAD'] = y_grad - var_names = ['x', 'mean', 'variance', 'y', 'y@GRAD'] - if has_scale: - var_names += ['scale'] - if has_bias: - var_names += ['bias'] - ground_truth = {name: var_dict[name] for name in var_names} - - program = base.Program() - with base.program_guard(program): - block = program.global_block() - for name in ground_truth: - block.create_var( - name=name, - dtype='float32', - shape=ground_truth[name].shape, - ) - inputs = {"X": block.var('x')} - fetch_list = [ - 'y', - 'mean', - 'variance', - 'x@GRAD', - ] - if has_scale: - inputs["Scale"] = block.var('scale') - fetch_list += ['scale@GRAD'] - if has_bias: - inputs["Bias"] = block.var('bias') - fetch_list += ['bias@GRAD'] - layer_norm_op = block.append_op( - type="layer_norm", - inputs=inputs, - outputs={ - "Y": block.var('y'), - "Mean": block.var('mean'), # share the same memory - "Variance": block.var( - 'variance' - ), # share the same memory - }, - attrs={ - "epsilon": epsilon, - "begin_norm_axis": begin_norm_axis, - "use_mkldnn": use_mkldnn, - }, - ) - # generate backward op_desc - grad_op_desc_list, op_grad_to_var = core.get_grad_op_desc( - layer_norm_op.desc, set(), [] - ) - grad_op_desc = grad_op_desc_list[0] - new_op_desc = block.desc.append_op() - new_op_desc.copy_from(grad_op_desc) - for var_name in grad_op_desc.output_arg_names(): - block.desc.var(var_name.encode("ascii")) - grad_op_desc.infer_var_type(block.desc) - grad_op_desc.infer_shape(block.desc) - for arg in grad_op_desc.output_arg_names(): - grad_var = block.desc.find_var(arg.encode("ascii")) - grad_var.set_dtype(core.VarDesc.VarType.FP32) - - program._sync_with_cpp() - exe = base.Executor(place) - name_list = ['x', 'y@GRAD'] - if has_scale: - name_list += ['scale'] - if has_bias: - name_list += ['bias'] - - out = exe.run( - program, - feed={name: var_dict[name] for name in name_list}, - fetch_list=fetch_list, - ) - # print(y) - # print(out[0]) - self.__assert_close(y, out[0], "y") - self.__assert_close(mean, out[1], "mean") - self.__assert_close(variance, out[2], "variance", 1e-3) - self.__assert_close(x_grad, out[3], "x_grad") - if has_scale: - self.__assert_close( - scale_grad, - out[fetch_list.index('scale@GRAD')], - "scale_grad", - 1e-3, - ) - if has_bias: - self.__assert_close( - bias_grad, - out[fetch_list.index('bias@GRAD')], - "bias_grad", - ) - - places = [core.CPUPlace()] - if ( - core.is_compiled_with_cuda() - and core.op_support_gpu("layer_norm") - and self.use_cudnn - ): - places.append(core.CUDAPlace(0)) - - for place in places: - test_with_place(place, shape, begin_norm_axis) - - def test_check_forward_backward_with_scale_and_bias(self): - self.check_forward_backward(shape=[2, 3, 4, 5], begin_norm_axis=1) - self.check_forward_backward(shape=[1, 3, 4, 5], begin_norm_axis=1) - self.check_forward_backward( - shape=[2, 3, 4, 5], - begin_norm_axis=1, - has_scale=False, - has_bias=True, - ) - self.check_forward_backward( - shape=[2, 3, 4, 5], - begin_norm_axis=1, - has_scale=True, - has_bias=False, - ) - self.check_forward_backward( - shape=[2, 3, 4, 5], - begin_norm_axis=1, - has_scale=False, - has_bias=False, - ) - self.check_forward_backward(shape=[2, 3, 4, 5], begin_norm_axis=3) - self.check_forward_backward( - shape=[92, 513, 129], begin_norm_axis=2, y_grad_scale=0.1 - ) - self.check_forward_backward(shape=[3, 34, 1134], begin_norm_axis=2) - self.check_forward_backward(shape=[3, 2, 1133], begin_norm_axis=2) - self.check_forward_backward( - shape=[92, 513, 1134], begin_norm_axis=2, y_grad_scale=0.1 - ) - self.check_forward_backward( - shape=[92, 513, 1134], - begin_norm_axis=2, - has_scale=False, - has_bias=True, - y_grad_scale=0.1, - ) - self.check_forward_backward( - shape=[92, 513, 1134], - begin_norm_axis=2, - has_scale=True, - has_bias=False, - y_grad_scale=0.1, - ) - self.check_forward_backward( - shape=[92, 513, 1134], - begin_norm_axis=2, - has_scale=False, - has_bias=False, - y_grad_scale=0.1, - ) - self.check_forward_backward( - shape=[512, 1024], begin_norm_axis=1, has_scale=True, has_bias=True - ) - self.check_forward_backward( - shape=[1, 128, 256, 256], - begin_norm_axis=3, - has_scale=True, - has_bias=True, - ) - self.check_forward_backward( - shape=[1, 256, 384], - begin_norm_axis=2, - has_scale=True, - has_bias=True, - ) - - -class TestLayerNormAPI(unittest.TestCase): - def test_case(self): - x = paddle.static.data(name='x', shape=[64, 32, 256], dtype='float32') - x = paddle.static.nn.layer_norm( - x, - scale=True, - shift=True, - begin_norm_axis=1, - epsilon=1e-05, - param_attr=None, - bias_attr=None, - ) - x = paddle.static.nn.layer_norm( - x, - scale=False, - shift=False, - begin_norm_axis=1, - epsilon=1e-05, - param_attr=None, - bias_attr=None, - ) - x = paddle.static.nn.layer_norm( - x, - scale=True, - shift=True, - begin_norm_axis=1, - epsilon=1e-05, - param_attr="scale", - bias_attr="shift", - ) - - class TestDygraphLayerNormAPIError(unittest.TestCase): def test_errors(self): with program_guard(Program(), Program()): diff --git a/test/legacy_test/test_nce.py b/test/legacy_test/test_nce.py new file mode 100644 index 00000000000000..c8a57ee5be4882 --- /dev/null +++ b/test/legacy_test/test_nce.py @@ -0,0 +1,154 @@ +# Copyright (c) 2018 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 op_test import OpTest + + +def nce( + input, weight, bias, sample_weight, labels, num_classes, num_sample_class +): + samples = [] + sample_labels = [] + batch_size = input.shape[0] + num_true_class = labels.shape[1] + for i in range(batch_size): + w = 1 if sample_weight is None else sample_weight[i] + for label in labels[i]: + samples.append((i, label, True, w)) + sample_labels.append(label) + for num in range(num_sample_class): + samples.append((i, num, False, w)) + sample_labels.append(num) + # forward bias + sample_out = np.zeros(len(samples)).astype(np.float32) + if bias is not None: + for i in range(len(samples)): + sample_out[i] = bias[samples[i][1]] + # forward weight + for i in range(len(samples)): + sample_out[i] += np.dot(input[samples[i][0]], weight[samples[i][1]]) + + # forward activation + sample_out = 1.0 / (1.0 + np.exp(-sample_out)) + # forward cost + out = np.zeros(batch_size).astype(np.float32) + b = 1.0 / num_classes * num_sample_class + for i in range(len(samples)): + o = sample_out[i] + cost = -np.log(o / (o + b)) if samples[i][2] else -np.log(b / (o + b)) + out[samples[i][0]] += cost * samples[i][3] + return ( + out[:, np.newaxis], + np.array(sample_out).reshape( + batch_size, num_sample_class + num_true_class + ), + np.array(sample_labels).reshape( + batch_size, num_sample_class + num_true_class + ), + ) + + +class TestNCE(OpTest): + def generate_data( + self, + dim, + batch_size, + num_classes, + num_true_class, + num_neg_samples, + is_sparse, + ): + input = np.random.randn(batch_size, dim).astype(np.float32) + weight = np.random.randn(num_classes, dim).astype(np.float32) + bias = np.random.randn(num_classes).astype(np.float32) + sample_weight = np.random.randn(batch_size).astype(np.float32) + labels = np.random.randint( + 0, num_classes, (batch_size, num_true_class) + ).astype("int64") + self.attrs = { + 'num_total_classes': num_classes, + 'num_neg_samples': num_neg_samples, + 'custom_neg_classes': list(range(num_neg_samples)), + 'seed': 0, + 'sampler': 0, + 'is_sparse': is_sparse, + 'is_test': self.is_test, + } + self.inputs = { + 'Input': input, + 'Label': labels, + 'Weight': weight, + 'Bias': bias, + 'SampleWeight': sample_weight, + } + + def set_is_test(self): + self.is_test = False + + def set_data(self): + self.generate_data(5, 25, 100, 1, 2, False) + + def compute(self): + out = nce( + self.inputs['Input'], + self.inputs['Weight'], + self.inputs['Bias'], + self.inputs['SampleWeight'], + self.inputs['Label'], + self.attrs['num_total_classes'], + self.attrs['num_neg_samples'], + ) + if self.is_test: + self.outputs = {'Cost': out[0]} + else: + self.outputs = { + 'Cost': out[0], + 'SampleLogits': out[1], + 'SampleLabels': out[2], + } + + def setUp(self): + self.op_type = 'nce' + self.set_is_test() + self.set_data() + self.compute() + + def test_check_output(self): + self.check_output() + + def test_check_grad(self): + self.check_grad( + ["Input", "Weight", "Bias"], "Cost", max_relative_error=0.02 + ) + + +class TestNCECase1Tensor(TestNCE): + def set_data(self): + self.generate_data(10, 20, 100, 2, 5, False) + + +class TestNCETensorIsTest(TestNCE): + # if is_test = True, there's no need to calculate grad + def set_is_test(self): + self.is_test = True + + def test_check_grad(self): + pass + + +if __name__ == '__main__': + unittest.main() diff --git a/test/legacy_test/test_optimizer.py b/test/legacy_test/test_optimizer.py new file mode 100644 index 00000000000000..63273c2eb99283 --- /dev/null +++ b/test/legacy_test/test_optimizer.py @@ -0,0 +1,169 @@ +# Copyright (c) 2018 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 tempfile +import unittest + +import numpy +import numpy as np + +import paddle +from paddle import base +from paddle.base import core +from paddle.base.framework import ( + convert_np_dtype_to_dtype_, +) +from paddle.io import Dataset + + +class TestOptimizerDtype(unittest.TestCase): + ''' + The dtype of optimizer should be inferred by parameters, and the learning rate + is cteated with the same dtype. + ''' + + def check_with_dtype(self, dtype): + class MyLayer(paddle.nn.Layer): + def __init__(self, dtype): + super().__init__() + self._w = self.create_parameter([2, 3], dtype=dtype) + self._b = self.create_parameter([2, 3], dtype=dtype) + + def forward(self, x): + return x * self._w + self._b + + with paddle.base.dygraph.guard(): + model = MyLayer(dtype) + x = paddle.rand([10, 2, 3], dtype=dtype) + loss = model(x) + adam = paddle.optimizer.Adam(parameters=model.parameters()) + loss.backward() + adam.step() + self.assertEqual(adam._dtype, convert_np_dtype_to_dtype_(dtype)) + + def test_float64(self): + self.check_with_dtype('float64') + + def test_float32(self): + self.check_with_dtype('float32') + + +@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 TestMasterWeightSaveForFP16(unittest.TestCase): + ''' + For Amp-O2, some optimizer(Momentum, Adam ...) will create master weights for parameters to improve the accuracy. + Master weights will be saved by optimizer::state_dict. + ''' + + def setUp(self): + self.temp_dir = tempfile.TemporaryDirectory() + + def tearDown(self): + self.temp_dir.cleanup() + + def check_with_opt_state_dict(self, use_save_load=True): + paddle.seed(100) + numpy.random.seed(100) + + class SimpleNet(paddle.nn.Layer): + def __init__(self, input_size, output_size): + super().__init__() + self.linears = paddle.nn.LayerList( + [ + paddle.nn.Linear(input_size, output_size) + for i in range(1) + ] + ) + + def forward(self, x): + for i, l in enumerate(self.linears): + x = self.linears[i](x) + return x + + input_size = 2 # 设为较大的值 + output_size = 2 # 设为较大的值 + batch_size = 2 # batch_size 为8的倍数 + nums_batch = 10 + + class RandomDataset(Dataset): + def __init__(self, num_samples): + self.num_samples = num_samples + + def __getitem__(self, idx): + data = numpy.random.random([input_size]).astype('float16') + label = numpy.random.random([output_size]).astype('float16') + return data, label + + def __len__(self): + return self.num_samples + + dataset = RandomDataset(nums_batch * batch_size) + loader = paddle.io.DataLoader( + dataset, + batch_size=batch_size, + shuffle=False, + drop_last=True, + num_workers=0, + ) + + mse = paddle.nn.MSELoss() + model = SimpleNet(input_size, output_size) # 定义模型 + optimizer = paddle.optimizer.Momentum( + learning_rate=0.0001, + parameters=model.parameters(), + multi_precision=True, + ) # 定义优化器 + scaler = paddle.amp.GradScaler(init_loss_scaling=1024) + model = paddle.amp.decorate(models=model, level='O2') + + for i, (data, label) in enumerate(loader): + with paddle.amp.auto_cast(level='O2'): + output = model(data) + loss = mse(output, label) + scaled = scaler.scale(loss) + scaled.backward() + scaler.step(optimizer) + scaler.update() + optimizer.clear_grad(set_to_zero=False) + + if use_save_load and i == 5: + model_path = os.path.join(self.temp_dir.name, "model.pdparams") + optimizer_path = os.path.join(self.temp_dir.name, "opt.pdopt") + paddle.save(model.state_dict(), model_path) + paddle.save(optimizer.state_dict(), optimizer_path) + model.set_state_dict(paddle.load(model_path)) + optimizer.set_state_dict(paddle.load(optimizer_path)) + + return loss.numpy() + + def test_with_state_dict(self): + if core.is_compiled_with_cuda(): + with base.dygraph.guard(): + out_use_state_dict = self.check_with_opt_state_dict( + use_save_load=True + ) + out_no_state_dict = self.check_with_opt_state_dict( + use_save_load=False + ) + np.testing.assert_array_equal(out_use_state_dict, out_no_state_dict) + + +if __name__ == '__main__': + paddle.enable_static() + unittest.main() diff --git a/test/deprecated/legacy_test/test_prelu_op.py b/test/legacy_test/test_prelu_op.py similarity index 88% rename from test/deprecated/legacy_test/test_prelu_op.py rename to test/legacy_test/test_prelu_op.py index 75bb9b69beed4a..aecc3af208225f 100644 --- a/test/deprecated/legacy_test/test_prelu_op.py +++ b/test/legacy_test/test_prelu_op.py @@ -20,7 +20,7 @@ import paddle import paddle.nn.functional as F from paddle import base -from paddle.base import Program, core +from paddle.base import core from paddle.pir_utils import test_with_pir_api @@ -481,65 +481,5 @@ def test_check_grad(self): create_test_bf16_class(TestModeElementRank3NHWC) create_test_bf16_class(TestModeElementRank6NHWC) - -def prelu_t(x, mode, param_attr=None, name=None, data_format='NCHW'): - 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( - attr=helper.param_attr, - shape=alpha_shape, - dtype='float32', - is_bias=False, - default_initializer=paddle.nn.initializer.Constant(0.25), - ) - out = helper.create_variable_for_type_inference(dtype) - helper.append_op( - type="prelu", - inputs={"X": x, 'Alpha': alpha}, - attrs={"mode": mode, 'data_format': data_format}, - outputs={"Out": out}, - ) - return out - - -# error message test if mode is not one of 'all', 'channel', 'element' -class TestModeError(unittest.TestCase): - def setUp(self): - self.place = ( - paddle.CUDAPlace(0) - if core.is_compiled_with_cuda() - else paddle.CPUPlace() - ) - self.x_np = np.ones([1, 2, 3, 4]).astype('float32') - - def test_mode_error(self): - 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') - except Exception as e: - assert e.args[0].find('InvalidArgument') != -1 - - def test_data_format_error1(self): - 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') - except Exception as e: - assert e.args[0].find('InvalidArgument') != -1 - - def test_data_format_error2(self): - 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') - except ValueError as e: - pass - - if __name__ == "__main__": unittest.main() diff --git a/test/deprecated/legacy_test/test_random_seed.py b/test/legacy_test/test_random_seed.py similarity index 87% rename from test/deprecated/legacy_test/test_random_seed.py rename to test/legacy_test/test_random_seed.py index ead15119a99225..8fbaf9a3d6942e 100644 --- a/test/deprecated/legacy_test/test_random_seed.py +++ b/test/legacy_test/test_random_seed.py @@ -359,57 +359,6 @@ def test_generator_randperm_static(self): np.testing.assert_allclose(out1_res2, out2_res2, rtol=1e-05) self.assertTrue(not np.allclose(out1_res2, out1_res1)) - def test_gen_TruncatedNormal_initializer(self): - base.disable_dygraph() - - gen = paddle.seed(123123143) - cur_state = gen.get_state() - - 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]) - result_1 = paddle.static.nn.fc( - x, - size=10, - weight_attr=paddle.nn.initializer.TruncatedNormal( - mean=0.0, std=2.0 - ), - ) - result_2 = paddle.static.nn.fc( - x, - size=10, - weight_attr=paddle.nn.initializer.TruncatedNormal( - mean=0.0, std=2.0 - ), - ) - - 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 base.program_guard(train_program, startup_program): - exe.run(startup_program) - out2 = exe.run( - train_program, feed={}, fetch_list=[result_1, result_2] - ) - - out1_res1 = np.array(out1[0]) - out1_res2 = np.array(out1[1]) - out2_res1 = np.array(out2[0]) - out2_res2 = np.array(out2[1]) - - if not core.is_compiled_with_cuda(): - print(">>>>>>> sampling id static >>>>>>>") - np.testing.assert_allclose(out1_res1, out2_res1, rtol=1e-05) - np.testing.assert_allclose(out1_res2, out2_res2, rtol=1e-05) - self.assertTrue(not np.allclose(out1_res2, out1_res1)) - if __name__ == "__main__": unittest.main() diff --git a/test/legacy_test/test_regularizer.py b/test/legacy_test/test_regularizer.py new file mode 100644 index 00000000000000..a85f2fcb075daf --- /dev/null +++ b/test/legacy_test/test_regularizer.py @@ -0,0 +1,217 @@ +# Copyright (c) 2018 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 contextlib +import random +import unittest + +import numpy as np + +import paddle +from paddle import base, regularizer +from paddle.base import core +from paddle.pir_utils import test_with_pir_api + + +class TestL1Decay(unittest.TestCase): + def test_l1decay_regularizer(self): + with paddle.pir_utils.IrGuard(): + main_program = paddle.static.Program() + with paddle.static.program_guard(main_program): + block = main_program.global_block() + mul_x = paddle.pir.core.create_parameter( + dtype="float32", + shape=[5, 10], + name="mul.x", + regularizer=regularizer.L1Decay(0.5), + initializer=paddle.nn.initializer.Constant(1), + ) + self.assertIsNotNone(mul_x.regularizer) + self.assertTrue( + isinstance(mul_x.regularizer, regularizer.L1Decay) + ) + + mul_y = paddle.static.data( + dtype="float32", shape=[10, 8], name="mul.y" + ) + mul_out = paddle.matmul(mul_x, mul_y) + mean_out = paddle.mean(mul_out) + grads = paddle.autograd.ir_backward.grad(mean_out, [mul_x]) + params_grads = [(mul_x, grads[0])] + self.assertEqual(len(params_grads), 1) + count_ops = len(block.ops) + optimizer = paddle.optimizer.Adam() + params_grads = optimizer.append_regularization_ops(params_grads) + self.assertEqual(len(params_grads), 1) + self.assertEqual(len(block.ops), count_ops + 5) + self.assertEqual(block.ops[-1].name(), 'pd_op.add_n') + self.assertEqual(block.ops[-3].name(), 'pd_op.scale') + self.assertEqual(block.ops[-5].name(), 'pd_op.sign') + + +class TestRegularizer(unittest.TestCase): + def setUp(self): + self.word_len = 1500 + self.train_data = [ + [(random.sample(range(1000), 10), [0])] for _ in range(2) + ] + + def get_places(self): + places = [core.CPUPlace()] + if core.is_compiled_with_cuda(): + places.append(core.CUDAPlace(0)) + return places + + @contextlib.contextmanager + def scope_prog_guard(self, 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 = base.Executor(place) + feeder = base.DataFeeder(feed_list=feed_list, place=place) + exe.run(base.default_startup_program()) + + main_prog = base.default_main_program() + param_list = [var.name for var in main_prog.block(0).all_parameters()] + + param_sum = [] + for data in self.train_data: + out = exe.run( + main_prog, feed=feeder.feed(data), fetch_list=param_list + ) + p_sum = 0 + for v in out: + p_sum += np.sum(np.abs(v)) + param_sum.append(p_sum) + return param_sum + + def check_l2decay_regularizer(self, place, model): + paddle.seed(1) + paddle.framework.random._manual_program_seed(1) + main_prog = base.framework.Program() + startup_prog = base.framework.Program() + with self.scope_prog_guard( + main_prog=main_prog, startup_prog=startup_prog + ): + data = paddle.static.data( + name="words", shape=[-1, 1], dtype="int64", lod_level=1 + ) + label = paddle.static.data( + name="label", shape=[-1, 1], dtype="int64" + ) + + avg_cost = model(data, label, self.word_len) + + optimizer = paddle.optimizer.Adagrad( + learning_rate=0.1, + weight_decay=paddle.regularizer.L2Decay(1.0), + ) + optimizer.minimize(avg_cost) + param_sum = self.run_program(place, [data, label]) + return param_sum + + def check_l2decay(self, place, model): + paddle.seed(1) + paddle.framework.random._manual_program_seed(1) + main_prog = base.framework.Program() + startup_prog = base.framework.Program() + + with self.scope_prog_guard( + main_prog=main_prog, startup_prog=startup_prog + ): + data = paddle.static.data( + name="words", shape=[-1, 1], dtype="int64", lod_level=1 + ) + label = paddle.static.data( + name="label", shape=[-1, 1], dtype="int64" + ) + + avg_cost_l2 = model(data, label, self.word_len) + + param_list = base.default_main_program().block(0).all_parameters() + para_sum = [] + for para in param_list: + para_mul = paddle.square(x=para) + para_sum.append(paddle.sum(para_mul)) + avg_cost_l2 += paddle.add_n(para_sum) * 0.5 + + optimizer = paddle.optimizer.Adagrad(learning_rate=0.1) + optimizer.minimize(avg_cost_l2) + param_sum = self.run_program(place, [data, label]) + return param_sum + + @test_with_pir_api + def test_repeated_regularization(self): + l1 = paddle.regularizer.L1Decay(coeff=0.1) + l2 = paddle.regularizer.L2Decay(coeff=0.01) + fc_param_attr = paddle.ParamAttr( + regularizer=paddle.regularizer.L1Decay() + ) + with paddle.static.program_guard( + paddle.static.Program(), paddle.static.Program() + ): + x = paddle.uniform([2, 2, 3]) + linear = paddle.nn.Linear(3, 5, weight_attr=fc_param_attr) + out = linear(x) + loss = paddle.sum(out) + sgd = paddle.optimizer.SGD(learning_rate=0.1, weight_decay=l2) + sgd.minimize(loss) + with base.dygraph.guard(): + input = paddle.to_tensor(np.random.randn(3, 2).astype('float32')) + paddle.seed(1) + paddle.framework.random._manual_program_seed(1) + + linear1 = paddle.nn.Linear( + 2, 2, weight_attr=fc_param_attr, bias_attr=fc_param_attr + ) + linear2 = paddle.nn.Linear( + 2, 2, weight_attr=fc_param_attr, bias_attr=fc_param_attr + ) + + loss1 = linear1(input) + loss1.backward() + # 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 base.ParamAttr + loss2 = linear2(input) + loss2.backward() + paddle.optimizer.SGD( + parameters=linear2.parameters(), learning_rate=1e-2 + ).minimize(loss2) + # they should both be applied by l1, and keep the same + np.testing.assert_allclose( + linear1.weight.numpy(), + linear2.weight.numpy(), + rtol=1e-05, + 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 base.ParamAttr!', + ) + + +if __name__ == '__main__': + unittest.main() diff --git a/test/deprecated/legacy_test/test_regularizer_api.py b/test/legacy_test/test_regularizer_api.py similarity index 77% rename from test/deprecated/legacy_test/test_regularizer_api.py rename to test/legacy_test/test_regularizer_api.py index 32a98c5a720915..f6d35076284632 100644 --- a/test/deprecated/legacy_test/test_regularizer_api.py +++ b/test/legacy_test/test_regularizer_api.py @@ -15,7 +15,6 @@ import contextlib import random import unittest -from functools import partial import numpy as np @@ -25,41 +24,6 @@ from paddle.pir_utils import test_with_pir_api -def bow_net( - data, - label, - dict_dim, - is_sparse=False, - emb_dim=8, - hid_dim=8, - hid_dim2=6, - class_dim=2, -): - """ - BOW net - This model is from https://github.com/PaddlePaddle/models: - base/PaddleNLP/text_classification/nets.py - """ - emb = paddle.static.nn.embedding( - input=data, is_sparse=is_sparse, size=[dict_dim, emb_dim] - ) - bow = paddle.static.nn.sequence_lod.sequence_pool( - input=emb, pool_type='sum' - ) - bow_tanh = paddle.tanh(bow) - fc_1 = paddle.static.nn.fc(x=bow_tanh, size=hid_dim, activation="tanh") - fc_2 = paddle.static.nn.fc(x=fc_1, size=hid_dim2, activation="tanh") - prediction = paddle.static.nn.fc( - x=[fc_2], size=class_dim, activation="softmax" - ) - cost = paddle.nn.functional.cross_entropy( - input=prediction, label=label, reduction='none', use_softmax=False - ) - avg_cost = paddle.mean(x=cost) - - return avg_cost - - class TestRegularizer(unittest.TestCase): def setUp(self): self.word_len = 1500 @@ -155,27 +119,6 @@ def check_l2decay(self, place, model): param_sum = self.run_program(place, [data, label]) return param_sum - def test_l2(self): - paddle.enable_static() - for place in self.get_places(): - dense_sparse_p_sum = [] - for sparse in [True, False]: - model = partial(bow_net, is_sparse=sparse) - framework_l2 = self.check_l2decay_regularizer(place, model) - l2 = self.check_l2decay(place, model) - assert len(l2) == len(framework_l2) - for i in range(len(l2)): - assert np.isclose(a=framework_l2[i], b=l2[i], rtol=5e-5) - dense_sparse_p_sum.append(framework_l2) - - assert len(dense_sparse_p_sum[0]) == len(dense_sparse_p_sum[1]) - for i in range(len(dense_sparse_p_sum[0])): - assert np.isclose( - a=dense_sparse_p_sum[0][i], - b=dense_sparse_p_sum[1][i], - rtol=5e-5, - ) - @test_with_pir_api def test_repeated_regularization(self): paddle.enable_static() diff --git a/test/deprecated/legacy_test/test_sgd_op.py b/test/legacy_test/test_sgd_op.py similarity index 60% rename from test/deprecated/legacy_test/test_sgd_op.py rename to test/legacy_test/test_sgd_op.py index 20f67faf44f3fe..d1e81b11c67e8b 100644 --- a/test/deprecated/legacy_test/test_sgd_op.py +++ b/test/legacy_test/test_sgd_op.py @@ -20,7 +20,6 @@ from utils import dygraph_guard import paddle -from paddle import base from paddle.base import core paddle.enable_static() @@ -204,32 +203,6 @@ def test_sparse_parameter_sgd(self): self.check_with_place(place) -class TestSGDOpWithLargeInput(unittest.TestCase): - def runTest(self): - paddle.enable_static() - data = paddle.tensor.fill_constant(shape=[1], value=128, dtype='int64') - label = paddle.tensor.fill_constant( - shape=[1, 150], value=0.5, dtype='float32' - ) - emb = paddle.static.nn.embedding( - input=data, size=(10000000, 150), dtype='float32' - ) - out = paddle.nn.functional.normalize(x=emb, axis=-1) - - cost = paddle.nn.functional.square_error_cost(input=out, label=label) - avg_cost = paddle.mean(cost) - sgd_optimizer = paddle.optimizer.SGD(learning_rate=0.001) - sgd_optimizer.minimize(avg_cost) - - 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]) - - class TestSGDV2(unittest.TestCase): def test_sgd_dygraph(self): paddle.disable_static() @@ -247,50 +220,6 @@ def test_sgd_dygraph(self): adam.step() adam.clear_gradients() - def test_sgd(self): - paddle.enable_static() - - def check_sgd_optimizer(optimizer_attr): - init_program = paddle.static.Program() - program = paddle.static.Program() - block = program.global_block() - mul_x = block.create_parameter( - dtype="float32", - shape=[5, 10], - lod_level=0, - name="mul.x", - optimize_attr=optimizer_attr, - ) - mul_y = block.create_var( - dtype="float32", shape=[10, 8], lod_level=0, name="mul.y" - ) - mul_out = block.create_var( - dtype="float32", shape=[5, 8], lod_level=0, name="mul.out" - ) - mean_out = block.create_var( - dtype="float32", shape=[1], lod_level=0, name="mean.out" - ) - block.append_op( - type="mul", - inputs={"X": mul_x, "Y": mul_y}, - outputs={"Out": mul_out}, - attrs={"x_num_col_dims": 1}, - ) - block.append_op( - type="mean", inputs={"X": mul_out}, outputs={"Out": mean_out} - ) - sgd_optimizer = paddle.optimizer.SGD(learning_rate=0.01) - opts, _ = sgd_optimizer.minimize(mean_out, init_program) - return opts - - opts = check_sgd_optimizer({'learning_rate': 1.1}) - self.assertEqual(len(opts), 2) - self.assertEqual([op.type for op in opts], ["scale", "sgd"]) - - opts = check_sgd_optimizer({'learning_rate': 1.0}) - self.assertEqual(len(opts), 1) - self.assertEqual([op.type for op in opts], ["sgd"]) - def test_raise_error(self): self.assertRaises(ValueError, paddle.optimizer.SGD, learning_rate=None) @@ -320,114 +249,6 @@ def test_sgd_group_dygraph(self): adam.clear_gradients() -class TestSGDMultiPrecision2_0(unittest.TestCase): - def dygraph_sgd_mp(self, mp): - paddle.disable_static() - paddle.seed(10) - paddle.set_device('gpu') - input = paddle.randn((2, 2)) - model = paddle.nn.Linear(2, 2) - optimizer = paddle.optimizer.SGD( - parameters=model.parameters(), multi_precision=mp - ) - if mp: - model = paddle.amp.decorate(models=model, level='O2') - scaler = paddle.amp.GradScaler(init_loss_scaling=1024) - - for idx in range(5): - if mp: - with paddle.amp.auto_cast(level='O2'): - output = model(input) - loss = paddle.mean(output) - scaled = scaler.scale(loss) - scaled.backward() - scaler.minimize(optimizer, scaled) - optimizer.clear_grad() - else: - output = model(input) - loss = paddle.mean(output) - optimizer.step() - optimizer.clear_grad() - - return output, model.parameters() - - def static_sgd_mp(self, mp): - paddle.enable_static() - paddle.seed(10) - np.random.seed(10) - exe = paddle.static.Executor('gpu') - train_program = paddle.static.Program() - startup_program = paddle.static.Program() - optimizer = paddle.optimizer.SGD(multi_precision=mp) - - if mp: - optimizer = paddle.static.amp.decorate( - optimizer, - init_loss_scaling=128.0, - use_dynamic_loss_scaling=True, - use_pure_fp16=True, - use_fp16_guard=False, - ) - with paddle.static.program_guard(train_program, startup_program): - if mp: - data = paddle.static.data( - shape=[2, 2], name='X', dtype='float16' - ) - else: - data = paddle.static.data( - shape=[2, 2], name='X', dtype='float32' - ) - hidden = paddle.static.nn.fc(x=data, size=10) - loss = paddle.mean(hidden) - optimizer.minimize(loss) - exe.run(startup_program) - - if mp: - optimizer.amp_init( - place=paddle.CUDAPlace(0), scope=paddle.static.global_scope() - ) - x = np.random.random(size=(2, 2)).astype('float16') - else: - x = np.random.random(size=(2, 2)).astype('float32') - out = [] - for idx in range(5): - (loss_data,) = exe.run( - train_program, feed={"X": x}, fetch_list=[loss] - ) - out.append(loss_data) - return out - - def test_main(self): - if not paddle.is_compiled_with_cuda(): - return - "Test dygraph mode" - output1_dy, params1_dy = self.dygraph_sgd_mp(mp=True) - output2_dy, params2_dy = self.dygraph_sgd_mp(mp=False) - np.testing.assert_allclose( - output1_dy.astype('float32').numpy(), - output2_dy.astype('float32').numpy(), - rtol=1e-05, - atol=0.1, - ) - for idx in range(len(params1_dy)): - np.testing.assert_allclose( - params1_dy[idx].astype('float32').numpy(), - params2_dy[idx].astype('float32').numpy(), - rtol=1e-05, - atol=0.1, - ) - "Test static graph mode" - output1_st = self.static_sgd_mp(mp=True) - output2_st = self.static_sgd_mp(mp=False) - for idx in range(len(output1_st)): - np.testing.assert_allclose( - output1_st[idx].astype('float32'), - output2_st[idx].astype('float32'), - rtol=1e-05, - atol=0.1, - ) - - class TestSGDSimple(unittest.TestCase): def setUp(self) -> None: self.data = np.random.random(size=(2, 2)).astype('float32') From 7742c90b4a8250e5f4796c73c818ede3604f9546 Mon Sep 17 00:00:00 2001 From: Wang Huan Date: Mon, 3 Jun 2024 08:29:47 +0000 Subject: [PATCH 07/20] refine --- test/deprecated/legacy_test/CMakeLists.txt | 1 + test/deprecated/legacy_test/test_adaptive_avg_pool2d.py | 2 +- test/deprecated/legacy_test/test_arg_min_max_op.py | 2 +- test/deprecated/legacy_test/test_eye_op.py | 2 +- test/deprecated/legacy_test/test_multinomial_op.py | 2 +- test/deprecated/legacy_test/test_squeeze2_op_rename.py | 2 +- 6 files changed, 6 insertions(+), 5 deletions(-) diff --git a/test/deprecated/legacy_test/CMakeLists.txt b/test/deprecated/legacy_test/CMakeLists.txt index a298606604b55d..12d4734020e2c9 100644 --- a/test/deprecated/legacy_test/CMakeLists.txt +++ b/test/deprecated/legacy_test/CMakeLists.txt @@ -802,3 +802,4 @@ set_tests_properties(test_linalg_matrix_exp PROPERTIES TIMEOUT 120) set_pir_tests_properties() set_tests_properties(test_reduce_as_op PROPERTIES TIMEOUT 30) +set_tests_properties(test_attribute_var_deprecated PROPERTIES TIMEOUT 100) diff --git a/test/deprecated/legacy_test/test_adaptive_avg_pool2d.py b/test/deprecated/legacy_test/test_adaptive_avg_pool2d.py index 880a7cf949a62e..5ed16ca8675b15 100644 --- a/test/deprecated/legacy_test/test_adaptive_avg_pool2d.py +++ b/test/deprecated/legacy_test/test_adaptive_avg_pool2d.py @@ -16,7 +16,7 @@ import unittest import numpy as np -from test_attribute_var import UnittestBase +from test_attribute_var_deprecated import UnittestBase import paddle from paddle.base import Program, core, program_guard diff --git a/test/deprecated/legacy_test/test_arg_min_max_op.py b/test/deprecated/legacy_test/test_arg_min_max_op.py index c35fa9f8f7d39b..69b98997aeed5e 100644 --- a/test/deprecated/legacy_test/test_arg_min_max_op.py +++ b/test/deprecated/legacy_test/test_arg_min_max_op.py @@ -17,7 +17,7 @@ import numpy as np from op_test import OpTest, convert_float_to_uint16 -from test_attribute_var import UnittestBase +from test_attribute_var_deprecated import UnittestBase import paddle from paddle.base import Program, program_guard diff --git a/test/deprecated/legacy_test/test_eye_op.py b/test/deprecated/legacy_test/test_eye_op.py index 41a4e6aea2f9d0..cafbfbd96beb04 100644 --- a/test/deprecated/legacy_test/test_eye_op.py +++ b/test/deprecated/legacy_test/test_eye_op.py @@ -17,7 +17,7 @@ import numpy as np from op_test import OpTest -from test_attribute_var import UnittestBase +from test_attribute_var_deprecated import UnittestBase import paddle from paddle import base diff --git a/test/deprecated/legacy_test/test_multinomial_op.py b/test/deprecated/legacy_test/test_multinomial_op.py index f6fc6e281193b8..48c00ed5506e54 100644 --- a/test/deprecated/legacy_test/test_multinomial_op.py +++ b/test/deprecated/legacy_test/test_multinomial_op.py @@ -17,7 +17,7 @@ import numpy as np from op_test import OpTest, convert_float_to_uint16 -from test_attribute_var import UnittestBase +from test_attribute_var_deprecated import UnittestBase import paddle from paddle import base diff --git a/test/deprecated/legacy_test/test_squeeze2_op_rename.py b/test/deprecated/legacy_test/test_squeeze2_op_rename.py index ed347eda7350bf..02e63c0cb24592 100644 --- a/test/deprecated/legacy_test/test_squeeze2_op_rename.py +++ b/test/deprecated/legacy_test/test_squeeze2_op_rename.py @@ -15,7 +15,7 @@ import os import unittest -from test_attribute_var import UnittestBase +from test_attribute_var_deprecated import UnittestBase import paddle from paddle.base.framework import Program, program_guard From 85c37dbbf4d22f309f86dd72d83e184a10e4ddbc Mon Sep 17 00:00:00 2001 From: Wang Huan Date: Mon, 3 Jun 2024 09:18:24 +0000 Subject: [PATCH 08/20] refine --- test/deprecated/legacy_test/CMakeLists.txt | 14 +++++++------- test/deprecated/quantization/CMakeLists.txt | 10 ---------- 2 files changed, 7 insertions(+), 17 deletions(-) diff --git a/test/deprecated/legacy_test/CMakeLists.txt b/test/deprecated/legacy_test/CMakeLists.txt index 75dc030c255530..4b6f44b13f0a80 100644 --- a/test/deprecated/legacy_test/CMakeLists.txt +++ b/test/deprecated/legacy_test/CMakeLists.txt @@ -508,8 +508,6 @@ endif() set_tests_properties(test_conv2d_api_deprecated PROPERTIES LABELS "RUN_TYPE=EXCLUSIVE") -set_tests_properties(test_conv_nn_grad_deprecated - PROPERTIES LABELS "RUN_TYPE=EXCLUSIVE") if(WITH_DISTRIBUTE) # FIXME(typhoonzero): add these tests back list(REMOVE_ITEM DIST_TEST_OPS "test_dist_transformer") @@ -806,16 +804,18 @@ foreach(PIR_COVERAGE_TEST ${PIR_COVERAGE_TESTS}) message(STATUS "PIR Copied OpTest: ${PIR_COVERAGE_TEST}_pir in legacy_test") endforeach() -set_tests_properties(test_decoupled_py_reader_static_build PROPERTIES TIMEOUT - 120) -set_tests_properties(test_fuse_bn_act_pass_static_build PROPERTIES TIMEOUT 120) +set_tests_properties(test_decoupled_py_reader_deprecated_static_build + PROPERTIES TIMEOUT 120) +set_tests_properties(test_fuse_bn_act_pass_deprecated_static_build + PROPERTIES TIMEOUT 120) set_tests_properties( - test_fuse_bn_act_pass_static_build + test_fuse_bn_act_pass_deprecated_static_build PROPERTIES ENVIRONMENT "FLAGS_cudnn_deterministic=1;FLAGS_cudnn_batchnorm_spatial_persistent=1;FLAGS_conv_workspace_size_limit=1000" ) -set_tests_properties(test_layer_norm_op_static_build PROPERTIES TIMEOUT 1500) +set_tests_properties(test_layer_norm_op_deprecated_static_build + PROPERTIES TIMEOUT 1500) set_tests_properties(test_reduce_op_static_build PROPERTIES TIMEOUT 500) py_test_modules(test_stride MODULES test_stride ENVS FLAGS_use_stride_kernel=true) diff --git a/test/deprecated/quantization/CMakeLists.txt b/test/deprecated/quantization/CMakeLists.txt index a5fc7d59e42f87..c1a97825f13117 100644 --- a/test/deprecated/quantization/CMakeLists.txt +++ b/test/deprecated/quantization/CMakeLists.txt @@ -195,11 +195,8 @@ if(WIN32) list(REMOVE_ITEM TEST_OPS test_llm_int8_linear) list(REMOVE_ITEM TEST_OPS test_quant_aware_deprecated) list(REMOVE_ITEM TEST_OPS test_quant_post_quant_aware) - list(REMOVE_ITEM TEST_OPS test_quant_post_quant_aware_deprecated) list(REMOVE_ITEM TEST_OPS test_quant_aware_user_defined) - list(REMOVE_ITEM TEST_OPS test_quant_aware_user_defined_deprecated) list(REMOVE_ITEM TEST_OPS test_quant_amp) - list(REMOVE_ITEM TEST_OPS test_quant_amp_deprecated) list(REMOVE_ITEM TEST_OPS test_apply_per_channel_scale) endif() @@ -243,7 +240,6 @@ if(WIN32) test_user_defined_quantization_deprecated test_quantization_scale_pass test_quantization_scale_pass_deprecated - test_quantization_pass test_quantization_pass_deprecated test_moving_average_abs_max_scale_op_deprecated test_imperative_qat_channelwise @@ -268,17 +264,11 @@ if(NOT WIN32) 120) set_tests_properties(test_quant_aware_deprecated PROPERTIES TIMEOUT 200) set_tests_properties(test_quant_post_quant_aware PROPERTIES TIMEOUT 200) - set_tests_properties(test_quant_post_quant_aware_deprecated PROPERTIES TIMEOUT - 200) set_tests_properties(test_quant_aware_user_defined PROPERTIES TIMEOUT 200) - set_tests_properties(test_quant_aware_user_defined_deprecated - PROPERTIES TIMEOUT 200) set_tests_properties(test_quant_amp PROPERTIES TIMEOUT 200) - set_tests_properties(test_quant_amp_deprecated PROPERTIES TIMEOUT 200) endif() set_tests_properties(test_graph_deprecated PROPERTIES TIMEOUT 120) -set_tests_properties(test_quantization_pass PROPERTIES TIMEOUT 120) set_tests_properties(test_quantization_pass_deprecated PROPERTIES TIMEOUT 120) set_tests_properties(test_imperative_qat_channelwise PROPERTIES TIMEOUT 200) set_tests_properties(test_user_defined_quantization PROPERTIES TIMEOUT 200) From 15743e8641eb134e1c8e4d0d21e916142b8030d2 Mon Sep 17 00:00:00 2001 From: Wang Huan Date: Mon, 3 Jun 2024 09:24:46 +0000 Subject: [PATCH 09/20] refine --- test/deprecated/legacy_test/CMakeLists.txt | 1 - test/deprecated/quantization/CMakeLists.txt | 2 -- 2 files changed, 3 deletions(-) diff --git a/test/deprecated/legacy_test/CMakeLists.txt b/test/deprecated/legacy_test/CMakeLists.txt index 4b6f44b13f0a80..69754af81c961a 100644 --- a/test/deprecated/legacy_test/CMakeLists.txt +++ b/test/deprecated/legacy_test/CMakeLists.txt @@ -684,7 +684,6 @@ set_tests_properties(test_sgd_op_deprecated PROPERTIES TIMEOUT 250) set_tests_properties(test_generator_dataloader_deprecated PROPERTIES TIMEOUT 120) set_tests_properties(test_reduce_op PROPERTIES TIMEOUT 500) -set_tests_properties(test_conv_nn_grad_deprecated PROPERTIES TIMEOUT 220) set_tests_properties(test_program_prune_backward_deprecated PROPERTIES TIMEOUT 120) set_tests_properties(test_imperative_optimizer_v2 PROPERTIES TIMEOUT 250) diff --git a/test/deprecated/quantization/CMakeLists.txt b/test/deprecated/quantization/CMakeLists.txt index c1a97825f13117..802e2a9b6bb436 100644 --- a/test/deprecated/quantization/CMakeLists.txt +++ b/test/deprecated/quantization/CMakeLists.txt @@ -236,7 +236,6 @@ list(REMOVE_ITEM TEST_OPS test_filter_pruning) # fix if(WIN32) set(SINGLE_CARD_TEST_OPS - test_user_defined_quantization test_user_defined_quantization_deprecated test_quantization_scale_pass test_quantization_scale_pass_deprecated @@ -271,7 +270,6 @@ endif() set_tests_properties(test_graph_deprecated PROPERTIES TIMEOUT 120) set_tests_properties(test_quantization_pass_deprecated PROPERTIES TIMEOUT 120) set_tests_properties(test_imperative_qat_channelwise PROPERTIES TIMEOUT 200) -set_tests_properties(test_user_defined_quantization PROPERTIES TIMEOUT 200) set_tests_properties(test_user_defined_quantization_deprecated PROPERTIES TIMEOUT 200) set_tests_properties(test_imperative_qat PROPERTIES TIMEOUT 200) From d5e5e3bfd08a2576349ec3dea7fa4d278b8939dd Mon Sep 17 00:00:00 2001 From: Wang Huan Date: Mon, 3 Jun 2024 11:22:48 +0000 Subject: [PATCH 10/20] refine --- .../legacy_test/test_adam_op_deprecated.py | 2 ++ .../legacy_test/test_backward_deprecated.py | 2 ++ .../test_conv3d_transpose_part2_op_deprecated.py | 2 ++ .../legacy_test/test_ema_fleet_deprecated.py | 2 ++ ..._executor_and_use_program_cache_deprecated.py | 2 ++ .../test_functional_conv2d_deprecated.py | 2 ++ .../test_functional_conv3d_deprecated.py | 2 ++ ...test_infer_no_need_buffer_slots_deprecated.py | 2 ++ .../legacy_test/test_optimizer_deprecated.py | 2 ++ .../test_program_prune_backward_deprecated.py | 2 ++ .../test_static_pylayer_deprecated.py | 8 +++++--- test/deprecated/quantization/CMakeLists.txt | 1 - .../rnn/test_rnn_nets_static_deprecated.py | 3 +++ .../test_imperative_hook_for_layer.py | 2 +- test/quantization/CMakeLists.txt | 13 +++---------- test/white_list/pir_op_test_white_list | 2 +- tools/parallel_UT_rule.py | 16 ++++++++-------- tools/static_mode_white_list.py | 8 ++++---- tools/windows/run_unittests.sh | 2 +- 19 files changed, 46 insertions(+), 29 deletions(-) diff --git a/test/deprecated/legacy_test/test_adam_op_deprecated.py b/test/deprecated/legacy_test/test_adam_op_deprecated.py index fd6970fae84628..357d2f9b438a4f 100644 --- a/test/deprecated/legacy_test/test_adam_op_deprecated.py +++ b/test/deprecated/legacy_test/test_adam_op_deprecated.py @@ -19,6 +19,8 @@ import paddle from paddle import base +paddle.enable_static() + class TestAdamOpV2(unittest.TestCase): def test_adam_op(self): diff --git a/test/deprecated/legacy_test/test_backward_deprecated.py b/test/deprecated/legacy_test/test_backward_deprecated.py index b3bef49bc1ab7c..64a3dfe7e778db 100644 --- a/test/deprecated/legacy_test/test_backward_deprecated.py +++ b/test/deprecated/legacy_test/test_backward_deprecated.py @@ -20,6 +20,8 @@ import paddle.nn.functional as F from paddle import base, static +paddle.enable_static() + class BackwardNet: """ diff --git a/test/deprecated/legacy_test/test_conv3d_transpose_part2_op_deprecated.py b/test/deprecated/legacy_test/test_conv3d_transpose_part2_op_deprecated.py index 7fcc24b5d67e7f..02e37f48cda2ef 100644 --- a/test/deprecated/legacy_test/test_conv3d_transpose_part2_op_deprecated.py +++ b/test/deprecated/legacy_test/test_conv3d_transpose_part2_op_deprecated.py @@ -23,6 +23,8 @@ from paddle import base from paddle.base import core +paddle.enable_static() + class TestConv3DTransposeAPI(unittest.TestCase): def test_case1(self): diff --git a/test/deprecated/legacy_test/test_ema_fleet_deprecated.py b/test/deprecated/legacy_test/test_ema_fleet_deprecated.py index e5ff36545f8189..0d19e5c7dabb00 100644 --- a/test/deprecated/legacy_test/test_ema_fleet_deprecated.py +++ b/test/deprecated/legacy_test/test_ema_fleet_deprecated.py @@ -19,6 +19,8 @@ import paddle from paddle import static, utils +paddle.enable_static() + def gen_data(): return np.random.random(size=(10, 5)).astype('float32') diff --git a/test/deprecated/legacy_test/test_executor_and_use_program_cache_deprecated.py b/test/deprecated/legacy_test/test_executor_and_use_program_cache_deprecated.py index d4f389f03b1c67..818bb1e48c3a6e 100644 --- a/test/deprecated/legacy_test/test_executor_and_use_program_cache_deprecated.py +++ b/test/deprecated/legacy_test/test_executor_and_use_program_cache_deprecated.py @@ -22,6 +22,8 @@ import paddle from paddle import base +paddle.enable_static() + class TestExecutor(unittest.TestCase): def test_mul(self): diff --git a/test/deprecated/legacy_test/test_functional_conv2d_deprecated.py b/test/deprecated/legacy_test/test_functional_conv2d_deprecated.py index 4e4ea507b092e5..790cebd22423cc 100644 --- a/test/deprecated/legacy_test/test_functional_conv2d_deprecated.py +++ b/test/deprecated/legacy_test/test_functional_conv2d_deprecated.py @@ -22,6 +22,8 @@ import paddle.nn.functional as F from paddle import base +paddle.enable_static() + class TestFunctionalConv2D(TestCase): batch_size = 4 diff --git a/test/deprecated/legacy_test/test_functional_conv3d_deprecated.py b/test/deprecated/legacy_test/test_functional_conv3d_deprecated.py index 75be067d4a261a..68e65a7db30b00 100644 --- a/test/deprecated/legacy_test/test_functional_conv3d_deprecated.py +++ b/test/deprecated/legacy_test/test_functional_conv3d_deprecated.py @@ -22,6 +22,8 @@ import paddle.nn.functional as F from paddle import base +paddle.enable_static() + class TestFunctionalConv3D(TestCase): batch_size = 4 diff --git a/test/deprecated/legacy_test/test_infer_no_need_buffer_slots_deprecated.py b/test/deprecated/legacy_test/test_infer_no_need_buffer_slots_deprecated.py index 1ba17a9270c503..d1286cbd02aba1 100644 --- a/test/deprecated/legacy_test/test_infer_no_need_buffer_slots_deprecated.py +++ b/test/deprecated/legacy_test/test_infer_no_need_buffer_slots_deprecated.py @@ -18,6 +18,8 @@ from paddle import base from paddle.base import core, framework +paddle.enable_static() + class TestInferNoNeedBufferSlots(unittest.TestCase): def net(self): diff --git a/test/deprecated/legacy_test/test_optimizer_deprecated.py b/test/deprecated/legacy_test/test_optimizer_deprecated.py index b3aa2e6c5f932f..f87f348d456ae7 100644 --- a/test/deprecated/legacy_test/test_optimizer_deprecated.py +++ b/test/deprecated/legacy_test/test_optimizer_deprecated.py @@ -25,6 +25,8 @@ program_guard, ) +paddle.enable_static() + class TestOptimizer(unittest.TestCase): def test_sgd_optimizer(self): diff --git a/test/deprecated/legacy_test/test_program_prune_backward_deprecated.py b/test/deprecated/legacy_test/test_program_prune_backward_deprecated.py index 8bf154f934b96c..dcd514b471415a 100755 --- a/test/deprecated/legacy_test/test_program_prune_backward_deprecated.py +++ b/test/deprecated/legacy_test/test_program_prune_backward_deprecated.py @@ -26,6 +26,8 @@ from paddle.base import core from paddle.dataset import wmt16 +paddle.enable_static() + DeviceType = core.DeviceType diff --git a/test/deprecated/legacy_test/test_static_pylayer_deprecated.py b/test/deprecated/legacy_test/test_static_pylayer_deprecated.py index f6db7f054cc16d..e15ba4ee363beb 100644 --- a/test/deprecated/legacy_test/test_static_pylayer_deprecated.py +++ b/test/deprecated/legacy_test/test_static_pylayer_deprecated.py @@ -13,14 +13,16 @@ # limitations under the License. import functools +import sys import unittest -from test.deprecated.legacy_test.test_prune_deprecated import ( + +sys.path.append(".") +import numpy as np +from test_prune_deprecated import ( TestExecutorRunAutoPrune, TestPruneBase, ) -import numpy as np - import paddle from paddle import base from paddle.base import core diff --git a/test/deprecated/quantization/CMakeLists.txt b/test/deprecated/quantization/CMakeLists.txt index 802e2a9b6bb436..baf4cf1c9a295f 100644 --- a/test/deprecated/quantization/CMakeLists.txt +++ b/test/deprecated/quantization/CMakeLists.txt @@ -237,7 +237,6 @@ list(REMOVE_ITEM TEST_OPS test_filter_pruning) if(WIN32) set(SINGLE_CARD_TEST_OPS test_user_defined_quantization_deprecated - test_quantization_scale_pass test_quantization_scale_pass_deprecated test_quantization_pass_deprecated test_moving_average_abs_max_scale_op_deprecated diff --git a/test/deprecated/rnn/test_rnn_nets_static_deprecated.py b/test/deprecated/rnn/test_rnn_nets_static_deprecated.py index 8e9554ecb93f4f..ef58211d65d667 100644 --- a/test/deprecated/rnn/test_rnn_nets_static_deprecated.py +++ b/test/deprecated/rnn/test_rnn_nets_static_deprecated.py @@ -210,6 +210,9 @@ def test_with_input_lengths(self): np.testing.assert_allclose(y1, y2, atol=1e-8, rtol=1e-5) np.testing.assert_allclose(h1, h2, atol=1e-8, rtol=1e-5) + def runTest(self): + self.test_with_input_lengths() + class TestLSTM(unittest.TestCase): def __init__(self, time_major=True, direction="forward", place="cpu"): diff --git a/test/legacy_test/test_imperative_hook_for_layer.py b/test/legacy_test/test_imperative_hook_for_layer.py index e80a31d47805f0..18335bfaf98f11 100644 --- a/test/legacy_test/test_imperative_hook_for_layer.py +++ b/test/legacy_test/test_imperative_hook_for_layer.py @@ -18,7 +18,7 @@ import numpy as np sys.path.append("../deprecated/legacy_test") -from test_imperative_lod_tensor_to_selected_rows import SimpleNet +from test_imperative_lod_tensor_to_selected_rows_deprecated import SimpleNet import paddle from paddle import base diff --git a/test/quantization/CMakeLists.txt b/test/quantization/CMakeLists.txt index e18f8c0a38096c..5b37f83e0c28f8 100644 --- a/test/quantization/CMakeLists.txt +++ b/test/quantization/CMakeLists.txt @@ -461,16 +461,9 @@ list(REMOVE_ITEM TEST_OPS test_filter_pruning) # fix if(WIN32) set(SINGLE_CARD_TEST_OPS - test_user_defined_quantization - test_quantization_scale_pass - test_quantization_pass - test_moving_average_abs_max_scale_op - test_imperative_qat_channelwise - test_imperative_qat - test_imperative_qat_lsq - test_imperative_qat_matmul - test_imperative_out_scale - test_graph) + test_imperative_qat_channelwise test_imperative_qat + test_imperative_qat_lsq test_imperative_qat_matmul + test_imperative_out_scale) list(REMOVE_ITEM TEST_OPS ${SINGLE_CARD_TEST_OPS}) foreach(src ${SINGLE_CARD_TEST_OPS}) py_test(${src} SRCS ${src}.py ENVS CUDA_VISIBLE_DEVICES=0) diff --git a/test/white_list/pir_op_test_white_list b/test/white_list/pir_op_test_white_list index 2daa7ddd497e45..99eb57ea4a17b2 100644 --- a/test/white_list/pir_op_test_white_list +++ b/test/white_list/pir_op_test_white_list @@ -142,7 +142,7 @@ test_i0_op test_i0e_op test_i1_op test_i1e_op -test_imperative_lod_tensor_to_selected_rows +test_imperative_lod_tensor_to_selected_rows_deprecated test_index_add_op test_index_sample_op test_index_select_op diff --git a/tools/parallel_UT_rule.py b/tools/parallel_UT_rule.py index 646a40b30dc0cb..2eaea8ad2c2858 100755 --- a/tools/parallel_UT_rule.py +++ b/tools/parallel_UT_rule.py @@ -66,7 +66,7 @@ 'test_paddle_inference_api', 'test_reference_count_pass_last_lived_ops', 'test_op_support_gpu', - 'test_conditional_block', + 'test_conditional_block_deprecated', 'test_fleet_rolemaker_init', 'test_pybind_interface', 'test_io_save_load', @@ -445,7 +445,7 @@ 'op_version_registry_test', 'test_cudnn_placement_pass', 'cipher_utils_test', - 'test_program_code', + 'test_program_code_deprecated', 'test_save_model_without_var', 'program_utils_test', 'test_fleet_distributed_strategy', @@ -918,7 +918,7 @@ 'test_nanmedian', 'test_linear', 'test_imperative_qat_amp', - 'test_truncated_gaussian_random_op', + 'test_truncated_gaussian_random_op_deprecated', 'test_lstm_cudnn_op', 'copy_same_tensor_test', 'test_squeeze2_op', @@ -1467,7 +1467,7 @@ 'test_trt_matmul', 'test_trt_fc_fuse_pass', 'test_trt_pad_op', - 'test_imperative_lod_tensor_to_selected_rows', + 'test_imperative_lod_tensor_to_selected_rows_deprecated', 'test_gru_unit_op', 'test_amp_check_finite_and_scale_op', 'test_imperative_selected_rows_to_lod_tensor', @@ -1693,7 +1693,7 @@ 'test_protobuf', 'test_progressbar', 'test_program_to_string', - 'test_program_code', + 'test_program_code_deprecated', 'test_program', 'test_precision_recall_op', 'test_post_training_quantization_resnet50', @@ -1880,7 +1880,7 @@ 'test_conv2d_bf16_mkldnn_op', 'test_context_manager', 'test_const_value', - 'test_conditional_block', + 'test_conditional_block_deprecated', 'test_concat_int8_mkldnn_op', 'test_concat_bf16_mkldnn_op', 'test_compat', @@ -2582,7 +2582,7 @@ 'test_save_model_without_var', 'test_unfold_op', 'test_conv_bn_fuse_pass', - 'test_truncated_gaussian_random_op', + 'test_truncated_gaussian_random_op_deprecated', 'test_traced_layer_err_msg', 'test_unique_with_counts', 'test_auc_single_pred_op', @@ -2689,7 +2689,7 @@ 'test_imperative_save_load', 'test_imperative_ptb_rnn_sorted_gradient', 'test_mul_op', - 'test_imperative_lod_tensor_to_selected_rows', + 'test_imperative_lod_tensor_to_selected_rows_deprecated', 'test_imperative_data_parallel', 'test_norm_nn_grad', 'test_im2sequence_op', diff --git a/tools/static_mode_white_list.py b/tools/static_mode_white_list.py index 8bfb9caef11c21..f5414d1ca34fae 100755 --- a/tools/static_mode_white_list.py +++ b/tools/static_mode_white_list.py @@ -96,7 +96,7 @@ 'test_compare_reduce_op', 'test_compiled_program', 'test_cond', - 'test_conditional_block', + 'test_conditional_block_deprecated', 'test_context_manager', 'test_conv1d_layer', 'test_conv1d_transpose_layer', @@ -252,7 +252,7 @@ 'test_imperative_gan', 'test_imperative_gnn', 'test_imperative_load_static_param', - 'test_imperative_lod_tensor_to_selected_rows', + 'test_imperative_lod_tensor_to_selected_rows_deprecated', 'test_imperative_optimizer', 'test_imperative_ptb_rnn', 'test_imperative_ptb_rnn_sorted_gradient', @@ -372,7 +372,7 @@ 'test_prior_box_op', 'test_profiler', 'test_program', - 'test_program_code', + 'test_program_code_deprecated', 'test_program_prune_backward', 'test_program_to_string', 'test_protobuf_descs', @@ -467,7 +467,7 @@ 'test_tril_triu_op', 'test_trilinear_interp_op', 'test_trilinear_interp_v2_op', - 'test_truncated_gaussian_random_op', + 'test_truncated_gaussian_random_op_deprecated', 'test_unbind_op', 'test_unfold_op', 'test_uniform_random_bf16_op', diff --git a/tools/windows/run_unittests.sh b/tools/windows/run_unittests.sh index ebca6e41296fd1..90f541851767b5 100644 --- a/tools/windows/run_unittests.sh +++ b/tools/windows/run_unittests.sh @@ -464,7 +464,7 @@ long_time_test="^test_gru_op$|\ ^test_cross_op$|\ ^test_elementwise_nn_grad$|\ ^test_fused_elemwise_activation_op$|\ -^test_imperative_lod_tensor_to_selected_rows$|\ +^test_imperative_lod_tensor_to_selected_rows_deprecated$|\ ^test_imperative_selected_rows_to_lod_tensor$|\ ^test_layer_norm_op$|\ ^test_layer_norm_op_static_build$|\ From 0a9a0be8862a64e5634e99cd15bf023aaedb56e0 Mon Sep 17 00:00:00 2001 From: Wang Huan Date: Tue, 4 Jun 2024 02:28:19 +0000 Subject: [PATCH 11/20] refine --- .../legacy_test/run_server_for_communicator_geo.py | 5 ++++- .../legacy_test/test_batch_norm_op_deprecated.py | 2 ++ .../legacy_test/test_dist_sparse_tensor_load_adagrad.py | 2 +- .../legacy_test/test_dist_sparse_tensor_load_adam.py | 2 +- .../legacy_test/test_dist_sparse_tensor_load_ftrl.py | 2 +- .../legacy_test/test_dist_sparse_tensor_load_momentum.py | 2 +- .../legacy_test/test_dist_sparse_tensor_load_rmsprop.py | 2 +- test/deprecated/quantization/test_quant_amp.py | 4 +++- .../quantization/test_quant_aware_user_defined.py | 8 +++++--- .../quantization/test_quant_post_quant_aware.py | 4 +++- test/legacy_test/run_server_for_communicator_geo.py | 2 +- 11 files changed, 23 insertions(+), 12 deletions(-) diff --git a/test/deprecated/legacy_test/run_server_for_communicator_geo.py b/test/deprecated/legacy_test/run_server_for_communicator_geo.py index 055ea5faf04fa9..c8a7ed8f8373e5 100644 --- a/test/deprecated/legacy_test/run_server_for_communicator_geo.py +++ b/test/deprecated/legacy_test/run_server_for_communicator_geo.py @@ -13,7 +13,10 @@ # limitations under the License. import os -from test.deprecated.legacy_test.test_communicator_geo_deprecated import ( +import sys + +sys.path.append(".") +from test_communicator_geo_deprecated import ( TestCommunicatorGeoEnd2End, ) diff --git a/test/deprecated/legacy_test/test_batch_norm_op_deprecated.py b/test/deprecated/legacy_test/test_batch_norm_op_deprecated.py index fa60d19ba56bdc..9c63d513e09d2c 100644 --- a/test/deprecated/legacy_test/test_batch_norm_op_deprecated.py +++ b/test/deprecated/legacy_test/test_batch_norm_op_deprecated.py @@ -24,6 +24,8 @@ from paddle import base from paddle.base import core +paddle.enable_static() + _set_use_system_allocator(True) diff --git a/test/deprecated/legacy_test/test_dist_sparse_tensor_load_adagrad.py b/test/deprecated/legacy_test/test_dist_sparse_tensor_load_adagrad.py index 35dd48accd42a0..518819cb155915 100644 --- a/test/deprecated/legacy_test/test_dist_sparse_tensor_load_adagrad.py +++ b/test/deprecated/legacy_test/test_dist_sparse_tensor_load_adagrad.py @@ -14,7 +14,7 @@ import unittest -from test_dist_sparse_tensor_load_sgd import TestSparseLoadProgram +from test_dist_sparse_tensor_load_sgd_deprecated import TestSparseLoadProgram import paddle from paddle import base diff --git a/test/deprecated/legacy_test/test_dist_sparse_tensor_load_adam.py b/test/deprecated/legacy_test/test_dist_sparse_tensor_load_adam.py index b5eae0e39807e0..88d28dc8cad524 100644 --- a/test/deprecated/legacy_test/test_dist_sparse_tensor_load_adam.py +++ b/test/deprecated/legacy_test/test_dist_sparse_tensor_load_adam.py @@ -14,7 +14,7 @@ import unittest -from test_dist_sparse_tensor_load_sgd import TestSparseLoadProgram +from test_dist_sparse_tensor_load_sgd_deprecated import TestSparseLoadProgram import paddle from paddle import base diff --git a/test/deprecated/legacy_test/test_dist_sparse_tensor_load_ftrl.py b/test/deprecated/legacy_test/test_dist_sparse_tensor_load_ftrl.py index 6a1f0175b1619e..a1cde59a3d7e0b 100644 --- a/test/deprecated/legacy_test/test_dist_sparse_tensor_load_ftrl.py +++ b/test/deprecated/legacy_test/test_dist_sparse_tensor_load_ftrl.py @@ -14,7 +14,7 @@ import unittest -from test_dist_sparse_tensor_load_sgd import TestSparseLoadProgram +from test_dist_sparse_tensor_load_sgd_deprecated import TestSparseLoadProgram import paddle from paddle import base diff --git a/test/deprecated/legacy_test/test_dist_sparse_tensor_load_momentum.py b/test/deprecated/legacy_test/test_dist_sparse_tensor_load_momentum.py index b7b590cbb3224d..35f8ad09d8f3f0 100644 --- a/test/deprecated/legacy_test/test_dist_sparse_tensor_load_momentum.py +++ b/test/deprecated/legacy_test/test_dist_sparse_tensor_load_momentum.py @@ -14,7 +14,7 @@ import unittest -from test_dist_sparse_tensor_load_sgd import TestSparseLoadProgram +from test_dist_sparse_tensor_load_sgd_deprecated import TestSparseLoadProgram import paddle from paddle import base diff --git a/test/deprecated/legacy_test/test_dist_sparse_tensor_load_rmsprop.py b/test/deprecated/legacy_test/test_dist_sparse_tensor_load_rmsprop.py index 9ce8e211f1e672..0150ae8027a35f 100644 --- a/test/deprecated/legacy_test/test_dist_sparse_tensor_load_rmsprop.py +++ b/test/deprecated/legacy_test/test_dist_sparse_tensor_load_rmsprop.py @@ -14,7 +14,7 @@ import unittest -from test_dist_sparse_tensor_load_sgd import TestSparseLoadProgram +from test_dist_sparse_tensor_load_sgd_deprecated import TestSparseLoadProgram import paddle from paddle import base diff --git a/test/deprecated/quantization/test_quant_amp.py b/test/deprecated/quantization/test_quant_amp.py index ac0f411f39855b..b708355a54827f 100644 --- a/test/deprecated/quantization/test_quant_amp.py +++ b/test/deprecated/quantization/test_quant_amp.py @@ -15,10 +15,12 @@ import logging import os +import sys import unittest -from test.deprecated.quantization.test_quant_aware_deprecated import MobileNet +sys.path.append(".") import numpy as np +from test_quant_aware_deprecated import MobileNet import paddle from paddle.static.quantization.quanter import convert, quant_aware diff --git a/test/deprecated/quantization/test_quant_aware_user_defined.py b/test/deprecated/quantization/test_quant_aware_user_defined.py index 5c7e48a854e68d..124836f560e6aa 100644 --- a/test/deprecated/quantization/test_quant_aware_user_defined.py +++ b/test/deprecated/quantization/test_quant_aware_user_defined.py @@ -13,14 +13,16 @@ # limitations under the License. import logging import os +import sys import unittest -from test.deprecated.quantization.test_quant_aware_deprecated import ( + +sys.path.append(".") +import numpy as np +from test_quant_aware_deprecated import ( MobileNet, StaticCase, ) -import numpy as np - import paddle from paddle.static.quantization.quanter import convert, quant_aware diff --git a/test/deprecated/quantization/test_quant_post_quant_aware.py b/test/deprecated/quantization/test_quant_post_quant_aware.py index d02c523f5b7c9f..db9e0a857f9d9f 100644 --- a/test/deprecated/quantization/test_quant_post_quant_aware.py +++ b/test/deprecated/quantization/test_quant_post_quant_aware.py @@ -14,10 +14,12 @@ import logging import random +import sys import unittest -from test.deprecated.quantization.test_quant_aware_deprecated import StaticCase +sys.path.append(".") import numpy as np +from test_quant_aware_deprecated import StaticCase import paddle from paddle.static.quantization.quanter import convert, quant_aware diff --git a/test/legacy_test/run_server_for_communicator_geo.py b/test/legacy_test/run_server_for_communicator_geo.py index 4f4173e5a2d0fd..31bdddda31a15c 100644 --- a/test/legacy_test/run_server_for_communicator_geo.py +++ b/test/legacy_test/run_server_for_communicator_geo.py @@ -16,7 +16,7 @@ import sys sys.path.append("../deprecated/legacy_test") -from test_communicator_geo import TestCommunicatorGeoEnd2End +from test_communicator_geo_deprecated import TestCommunicatorGeoEnd2End import paddle From e805637b8a5928eaec566f6b465b9851f0aada3d Mon Sep 17 00:00:00 2001 From: Wang Huan Date: Tue, 4 Jun 2024 02:40:37 +0000 Subject: [PATCH 12/20] refine --- test/deprecated/legacy_test/CMakeLists.txt | 2 -- 1 file changed, 2 deletions(-) diff --git a/test/deprecated/legacy_test/CMakeLists.txt b/test/deprecated/legacy_test/CMakeLists.txt index adeb6390e43800..d355f1ca4d7f63 100644 --- a/test/deprecated/legacy_test/CMakeLists.txt +++ b/test/deprecated/legacy_test/CMakeLists.txt @@ -645,10 +645,8 @@ set_tests_properties(test_imperative_lod_tensor_to_selected_rows_deprecated set_tests_properties(test_deformable_conv_op_deprecated PROPERTIES TIMEOUT 200) set_tests_properties(test_regularizer_api_deprecated PROPERTIES TIMEOUT 150) -set_tests_properties(test_deformable_conv_op PROPERTIES TIMEOUT 200) set_tests_properties(test_inplace_softmax_with_cross_entropy PROPERTIES TIMEOUT 120) -set_tests_properties(test_regularizer_api PROPERTIES TIMEOUT 150) if(NOT WIN32) if(WITH_NV_JETSON) set_tests_properties(test_ir_memory_optimize_nlp PROPERTIES TIMEOUT 1200) From c68e1ea602abbf75deb20bfcf9a64a062760ecae Mon Sep 17 00:00:00 2001 From: Wang Huan Date: Tue, 4 Jun 2024 04:41:49 +0000 Subject: [PATCH 13/20] refine --- test/deprecated/legacy_test/test_fleet_base_deprecated.py | 4 ---- 1 file changed, 4 deletions(-) diff --git a/test/deprecated/legacy_test/test_fleet_base_deprecated.py b/test/deprecated/legacy_test/test_fleet_base_deprecated.py index 8fa62c24e3a4cf..496bae74420618 100644 --- a/test/deprecated/legacy_test/test_fleet_base_deprecated.py +++ b/test/deprecated/legacy_test/test_fleet_base_deprecated.py @@ -12,7 +12,6 @@ # See the License for the specific language governing permissions and # limitations under the License. -import os import unittest import numpy as np @@ -23,9 +22,6 @@ class TestFleetBaseSingleError(unittest.TestCase): - def setUp(self): - os.environ.pop("PADDLE_TRAINER_ENDPOINTS") - def gen_data(self): return { "x": np.random.random(size=(128, 32)).astype('float32'), From 6171eceec9160098f23dc14afa1a1246157aeef6 Mon Sep 17 00:00:00 2001 From: Wang Huan Date: Tue, 4 Jun 2024 07:43:42 +0000 Subject: [PATCH 14/20] refine --- tools/parallel_UT_rule.py | 2 +- tools/static_mode_white_list.py | 2 +- tools/windows/run_unittests.sh | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/tools/parallel_UT_rule.py b/tools/parallel_UT_rule.py index 26218560c7ea16..1bb3ef1f764a5e 100755 --- a/tools/parallel_UT_rule.py +++ b/tools/parallel_UT_rule.py @@ -1233,7 +1233,7 @@ 'test_memory_reuse_exclude_feed_var', 'test_polygon_box_transform', 'math_function_gpu_test', - 'test_program_prune_backward', + 'test_program_prune_backward_deprecated', 'test_ema_fleet', 'test_fleet_amp_init', 'test_normalize', diff --git a/tools/static_mode_white_list.py b/tools/static_mode_white_list.py index e0e4f039df2ca5..e16c6c1fd494fe 100755 --- a/tools/static_mode_white_list.py +++ b/tools/static_mode_white_list.py @@ -373,7 +373,7 @@ 'test_profiler', 'test_program', 'test_program_code_deprecated', - 'test_program_prune_backward', + 'test_program_prune_backward_deprecated', 'test_program_to_string', 'test_protobuf_descs', 'test_proximal_gd_op', diff --git a/tools/windows/run_unittests.sh b/tools/windows/run_unittests.sh index 90f541851767b5..141e1c13ff5617 100644 --- a/tools/windows/run_unittests.sh +++ b/tools/windows/run_unittests.sh @@ -24,7 +24,7 @@ disable_wingpu_test="^test_model$|\ ^test_generator_dataloader_deprecated$|\ ^test_parallel_dygraph_sync_batch_norm$|\ ^test_py_reader_using_executor$|\ -^test_program_prune_backward$|\ +^test_program_prune_backward_deprecated$|\ ^test_decoupled_py_reader_data_check$|\ ^test_fleet_base_single$|\ ^test_multiprocess_dataloader_iterable_dataset_dynamic$|\ @@ -436,7 +436,7 @@ disable_wincpu_test="^jit_kernel_test$|\ ^test_vision_models$|\ ^test_dygraph_multi_forward$|\ ^test_imperative_transformer_sorted_gradient$|\ -^test_program_prune_backward$|\ +^test_program_prune_backward_deprecated$|\ ^test_imperative_resnet$|\ ^test_imperative_resnet_sorted_gradient$|\ ^test_imperative_se_resnext$|\ From 480679bf358db8c17b92e714dfd939c5702aa425 Mon Sep 17 00:00:00 2001 From: Wang Huan Date: Tue, 4 Jun 2024 07:59:03 +0000 Subject: [PATCH 15/20] refine --- test/deprecated/legacy_test/CMakeLists.txt | 1 + test/deprecated/quantization/CMakeLists.txt | 3 +++ tools/parallel_UT_rule.py | 8 ++++---- tools/windows/run_unittests.sh | 4 ++-- 4 files changed, 10 insertions(+), 6 deletions(-) diff --git a/test/deprecated/legacy_test/CMakeLists.txt b/test/deprecated/legacy_test/CMakeLists.txt index d355f1ca4d7f63..61046057f7c7a1 100644 --- a/test/deprecated/legacy_test/CMakeLists.txt +++ b/test/deprecated/legacy_test/CMakeLists.txt @@ -824,3 +824,4 @@ set_pir_tests_properties() set_tests_properties(test_reduce_as_op PROPERTIES TIMEOUT 30) set_tests_properties(test_attribute_var_deprecated PROPERTIES TIMEOUT 100) +set_tests_properties(test_inference_api_deprecated PROPERTIES TIMEOUT 100) diff --git a/test/deprecated/quantization/CMakeLists.txt b/test/deprecated/quantization/CMakeLists.txt index baf4cf1c9a295f..0ab38193a8c09c 100644 --- a/test/deprecated/quantization/CMakeLists.txt +++ b/test/deprecated/quantization/CMakeLists.txt @@ -280,3 +280,6 @@ if(APPLE) 300) set_tests_properties(test_imperative_skip_op PROPERTIES TIMEOUT 300) endif() + +set_tests_properties(test_quantization_scale_pass_deprecated PROPERTIES TIMEOUT + 100) diff --git a/tools/parallel_UT_rule.py b/tools/parallel_UT_rule.py index 1bb3ef1f764a5e..f6d55c6787994f 100755 --- a/tools/parallel_UT_rule.py +++ b/tools/parallel_UT_rule.py @@ -228,7 +228,7 @@ 'test_analyzer_capi_exp_pd_threads', 'test_selected_rows', 'test_fleet_sharding_meta_optimizer', - 'test_inference_api', + 'test_inference_api_deprecated', 'test_data_generator', 'test_deprecated_memory_optimize_interfaces', 'test_ir_skip_layernorm_pass', @@ -1266,7 +1266,7 @@ 'test_nn_margin_rank_loss', 'test_arg_min_max_v2_op', 'test_variance_layer', - 'test_quantization_scale_pass', + 'test_quantization_scale_pass_deprecated', 'test_segment_ops', 'test_layers', 'test_imperative_qat_channelwise', @@ -1756,7 +1756,7 @@ 'test_infer_shape', 'test_infer_no_need_buffer_slots', 'test_inference_model_io', - 'test_inference_api', + 'test_inference_api_deprecated', 'test_imperative_signal_handler', 'test_imperative_numpy_bridge', 'test_imperative_group', @@ -2856,7 +2856,7 @@ 'test_eager_tensor', 'trt_split_converter_test', 'test_user_defined_quantization', - 'test_quantization_scale_pass', + 'test_quantization_scale_pass_deprecated', 'feed_forward_test', 'test_standalone_executor', 'test_imperative_qat_user_defined', diff --git a/tools/windows/run_unittests.sh b/tools/windows/run_unittests.sh index 141e1c13ff5617..52ce0a10dfce38 100644 --- a/tools/windows/run_unittests.sh +++ b/tools/windows/run_unittests.sh @@ -184,7 +184,7 @@ disable_wingpu_cuda12_test="^test_cholesky_op$|\ ^test_functional_conv3d$|\ ^test_functional_conv3d_transpose$|\ ^test_imperative_layer_children$|\ -^test_inference_api$|\ +^test_inference_api_deprecated$|\ ^test_trans_layout_op$|\ ^test_pool2d_op$|\ ^test_conv3d_transpose_op$|\ @@ -211,7 +211,7 @@ disable_wingpu_cuda12_test="^test_cholesky_op$|\ ^test_callback_visualdl$|\ ^test_callback_wandb$|\ ^test_user_defined_quantization$|\ -^test_quantization_scale_pass$|\ +^test_quantization_scale_pass_deprecated$|\ ^test_quantization_pass$|\ ^test_imperative_qat$|\ ^test_graph$|\ From aa3871189af6967c883f12f8b54c0ee3baf933cb Mon Sep 17 00:00:00 2001 From: Wang Huan Date: Wed, 5 Jun 2024 01:41:55 +0000 Subject: [PATCH 16/20] refine --- .../test_standalone_executor.py | 115 ++++++++++++ .../test_standalone_executor_deprecated.py | 175 ------------------ test/mkldnn/test_batch_norm_mkldnn_op.py | 4 +- tools/parallel_UT_rule.py | 28 +-- tools/static_mode_white_list.py | 12 +- tools/windows/run_unittests.sh | 48 ++--- 6 files changed, 161 insertions(+), 221 deletions(-) delete mode 100644 test/deprecated/standalone_executor/test_standalone_executor_deprecated.py diff --git a/test/deprecated/standalone_executor/test_standalone_executor.py b/test/deprecated/standalone_executor/test_standalone_executor.py index c4b86d4284454f..934558c170f513 100644 --- a/test/deprecated/standalone_executor/test_standalone_executor.py +++ b/test/deprecated/standalone_executor/test_standalone_executor.py @@ -20,6 +20,7 @@ import unittest import numpy as np +from utils import static_guard import paddle from paddle.base import core @@ -136,6 +137,120 @@ def run_test(self, use_new_executor=True): return outs +class SwitchExecutorInterfaceWithFeed(unittest.TestCase): + def setUp(self): + self.place = ( + paddle.CUDAPlace(0) + if core.is_compiled_with_cuda() + else paddle.CPUPlace() + ) + self.iter_run = 2 + + def build_program(self, is_double=False): + main_program = paddle.static.Program() + startup_program = paddle.static.Program() + with paddle.static.program_guard(main_program, startup_program): + a = paddle.static.data(name="a", shape=[2, 2], dtype='float32') + b = paddle.ones([2, 2]) * 2 + t = paddle.static.nn.fc(a, 2) + c = t + b + if is_double: + c = c + c + + return main_program, startup_program, [c] + + def _run( + self, + feed, + use_str=False, + is_double=False, + add_wrong_fetch=False, + use_compiled=False, + ): + paddle.seed(2020) + + main_program, startup_program, fetch_vars = self.build_program( + is_double + ) + + exe = paddle.static.Executor(self.place) + exe.run(startup_program) + + if use_compiled: + main_program = paddle.static.CompiledProgram(main_program) + + if use_str: # test for fetch name + fetch_vars = [x.name for x in fetch_vars] + if add_wrong_fetch: # test for wrong fetch type + fetch_vars.append(1123) + outs = [] + for i in range(self.iter_run): + out = exe.run(main_program, feed=feed, fetch_list=fetch_vars)[0] + + outs.append(out) + + return outs + + def run_dygraph(self, feed): + def run_once(is_double): + paddle.seed(2020) + a = feed['a'] + a = paddle.to_tensor(a, dtype='float32') + b = paddle.ones([2, 2]) * 2 + t = paddle.nn.Linear(2, 2)(a) + c = t + b + if is_double: + c = c + c + return c.numpy() + + out1 = [] + for i in range(self.iter_run): + out1.append(run_once(False)) + out2 = [] + for i in range(self.iter_run): + out2.append(run_once(True)) + return [out1, out2] + + def run_new_executor(self, feed, use_compiled=False): + # run construct program 1 + out1 = self._run( + feed, use_str=False, is_double=False, use_compiled=use_compiled + ) + # run construct program 2 with same executor + out2 = self._run( + feed, use_str=True, is_double=True, use_compiled=use_compiled + ) + + return [out1, out2] + + def test_with_feed(self): + data = np.ones([2, 2], dtype="float32") + feed = {"a": data, 'fake_input': data} + + with static_guard(): + res = self.run_new_executor(feed) + with paddle.base.dygraph.guard(): + gt = self.run_dygraph(feed) + for x, y in zip(gt, res): + np.testing.assert_array_equal(x, y) + + def test_with_error(self): + feed = [{'a': np.ones([2, 2], dtype="float32")}] + + with self.assertRaises(TypeError): + self._run(feed[0], add_wrong_fetch=True) + + def test_empty_program(self): + program = paddle.static.Program() + exe = paddle.static.Executor(self.place) + for i in range(10): + out = exe.run() # old executor + + for i in range(10): + print(i, flush=1) + out = exe.run(program, feed=None) + + class TestException(unittest.TestCase): def setUp(self): self.place = paddle.CPUPlace() diff --git a/test/deprecated/standalone_executor/test_standalone_executor_deprecated.py b/test/deprecated/standalone_executor/test_standalone_executor_deprecated.py deleted file mode 100644 index 930cc4c28907cb..00000000000000 --- a/test/deprecated/standalone_executor/test_standalone_executor_deprecated.py +++ /dev/null @@ -1,175 +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. - -import os - -os.environ['FLAGS_use_stream_safe_cuda_allocator'] = "true" -import unittest - -import numpy as np -from utils import static_guard - -import paddle -from paddle.base import core - -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): - with paddle.static.device_guard('cpu'): - data = paddle.ones([4, 64], dtype='float32', name='data') - - # data -> [memcpy_h2d] -> data' -> [matmul] -> out ->[add] -> add_out - with paddle.static.device_guard('gpu'): - weight = paddle.randn([64, 64], name='weight') # gpu - matmul_out = paddle.matmul(data, weight, name='matmul_out') # gpus - bias = paddle.ones([4, 64], dtype='float32', name='bias') - add_out = paddle.add(matmul_out, bias, name='add_out') - - # add_out -> [memcpy_d2h] -> add_out' -> [sub] -> sub_out -> [tanh] -> tanh_out - with paddle.static.device_guard('cpu'): - sub_out = paddle.subtract(add_out, data, name='sub_out') - tanh_out = paddle.tanh(sub_out, name='tanh_out') - - with paddle.static.device_guard('gpu'): - 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 = paddle.add(out_before, out_last, name='out') - mean = paddle.mean(out, name='mean_out') - - return main_program, startup_program, [mean] - - -class SwitchExecutorInterfaceWithFeed(unittest.TestCase): - def setUp(self): - self.place = ( - paddle.CUDAPlace(0) - if core.is_compiled_with_cuda() - else paddle.CPUPlace() - ) - self.iter_run = 2 - - def build_program(self, is_double=False): - main_program = paddle.static.Program() - startup_program = paddle.static.Program() - with paddle.static.program_guard(main_program, startup_program): - a = paddle.static.data(name="a", shape=[2, 2], dtype='float32') - b = paddle.ones([2, 2]) * 2 - t = paddle.static.nn.fc(a, 2) - c = t + b - if is_double: - c = c + c - - return main_program, startup_program, [c] - - def _run( - self, - feed, - use_str=False, - is_double=False, - add_wrong_fetch=False, - use_compiled=False, - ): - paddle.seed(2020) - - main_program, startup_program, fetch_vars = self.build_program( - is_double - ) - - exe = paddle.static.Executor(self.place) - exe.run(startup_program) - - if use_compiled: - main_program = paddle.static.CompiledProgram(main_program) - - if use_str: # test for fetch name - fetch_vars = [x.name for x in fetch_vars] - if add_wrong_fetch: # test for wrong fetch type - fetch_vars.append(1123) - outs = [] - for i in range(self.iter_run): - out = exe.run(main_program, feed=feed, fetch_list=fetch_vars)[0] - - outs.append(out) - - return outs - - def run_dygraph(self, feed): - def run_once(is_double): - paddle.seed(2020) - a = feed['a'] - a = paddle.to_tensor(a, dtype='float32') - b = paddle.ones([2, 2]) * 2 - t = paddle.nn.Linear(2, 2)(a) - c = t + b - if is_double: - c = c + c - return c.numpy() - - out1 = [] - for i in range(self.iter_run): - out1.append(run_once(False)) - out2 = [] - for i in range(self.iter_run): - out2.append(run_once(True)) - return [out1, out2] - - def run_new_executor(self, feed, use_compiled=False): - # run construct program 1 - out1 = self._run( - feed, use_str=False, is_double=False, use_compiled=use_compiled - ) - # run construct program 2 with same executor - out2 = self._run( - feed, use_str=True, is_double=True, use_compiled=use_compiled - ) - - return [out1, out2] - - def test_with_feed(self): - data = np.ones([2, 2], dtype="float32") - feed = {"a": data, 'fake_input': data} - - with static_guard(): - res = self.run_new_executor(feed) - with paddle.base.dygraph.guard(): - gt = self.run_dygraph(feed) - for x, y in zip(gt, res): - np.testing.assert_array_equal(x, y) - - def test_with_error(self): - feed = [{'a': np.ones([2, 2], dtype="float32")}] - - with self.assertRaises(TypeError): - self._run(feed[0], add_wrong_fetch=True) - - def test_empty_program(self): - program = paddle.static.Program() - exe = paddle.static.Executor(self.place) - for i in range(10): - out = exe.run() # old executor - - for i in range(10): - print(i, flush=1) - out = exe.run(program, feed=None) - - -if __name__ == "__main__": - unittest.main() diff --git a/test/mkldnn/test_batch_norm_mkldnn_op.py b/test/mkldnn/test_batch_norm_mkldnn_op.py index 99f48c65b0a4e6..490021ecee6d97 100644 --- a/test/mkldnn/test_batch_norm_mkldnn_op.py +++ b/test/mkldnn/test_batch_norm_mkldnn_op.py @@ -20,8 +20,8 @@ from op_test import _set_use_system_allocator, pir_executor_guard sys.path.append("../deprecated/legacy_test") -from test_batch_norm_op import ( - TestBatchNormOpInference, +from test_batch_norm_op import TestBatchNormOpInference +from test_batch_norm_op_deprecated import ( TestBatchNormOpTraining, _reference_grad, _reference_training, diff --git a/tools/parallel_UT_rule.py b/tools/parallel_UT_rule.py index f6d55c6787994f..503209472304d7 100755 --- a/tools/parallel_UT_rule.py +++ b/tools/parallel_UT_rule.py @@ -503,8 +503,8 @@ 'test_dist_fleet_heter_program', 'test_dist_fleet_ctr', 'test_collective_allreduce_api', - 'test_dataloader_unkeep_order', - 'test_dataloader_keep_order', + 'test_dataloader_unkeep_order_deprecated', + 'test_dataloader_keep_order_deprecated', 'test_dist_se_resnext_sync', 'test_dist_fleet_ps6', 'test_dist_fleet_a_sync_optimizer_auto_async', @@ -1081,7 +1081,7 @@ 'test_prelu_op', 'test_l1_norm_op', 'test_rmsprop_op', - 'test_fuse_bn_act_pass', + 'test_fuse_bn_act_pass_deprecated', 'test_inplace_addto_strategy', 'test_paddle_save_load', 'test_prelu_mkldnn_op', @@ -1275,7 +1275,7 @@ 'test_l1_loss', 'test_ifelse', 'test_cache_program', - 'test_ir_fc_fuse_pass', + 'test_ir_fc_fuse_pass_deprecated', 'test_kldiv_loss_op', 'test_switch_case', 'test_unique', @@ -1332,7 +1332,7 @@ 'test_error', 'test_callbacks', 'test_imperative_recurrent_usage', - 'test_deform_conv2d', + 'test_deform_conv2d_deprecated', 'test_coalesce_tensor_op', 'test_tsm', 'test_fused_multihead_matmul_op', @@ -1383,7 +1383,7 @@ 'test_deformable_psroi_pooling', 'test_multi_precision_fp16_train', 'test_adam_op_multi_thread', - 'test_decoupled_py_reader', + 'test_decoupled_py_reader_deprecated', 'test_distribute_fpn_proposals_op', 'transform_test', 'test_nan_inf', @@ -1472,7 +1472,7 @@ 'test_gru_unit_op', 'test_amp_check_finite_and_scale_op', 'test_imperative_selected_rows_to_lod_tensor', - 'test_add_reader_dependency', + 'test_add_reader_dependency_deprecated', 'test_imperative_transformer_sorted_gradient', 'test_bicubic_interp_v2_op', 'test_rank_attention_op', @@ -2260,7 +2260,7 @@ 'test_reshape_op', 'test_fused_transformer_encoder_layer', 'test_eager_deletion_while_op', - 'test_dataloader_unkeep_order', + 'test_dataloader_unkeep_order_deprecated', 'test_correlation', 'test_moving_average_abs_max_scale_op', 'test_flatten_contiguous_range_op', @@ -2293,7 +2293,7 @@ 'test_imperative_trace_non_persistable_inputs', 'test_executor_return_tensor_not_overwriting', 'test_density_prior_box_op', - 'test_dataloader_keep_order', + 'test_dataloader_keep_order_deprecated', 'test_bce_loss', 'test_fetch_lod_tensor_array', 'test_smooth_l1_loss', @@ -2348,7 +2348,7 @@ 'test_diagonal_op', 'test_diagflat', 'test_determinant_op', - 'test_deform_conv2d', + 'test_deform_conv2d_deprecated', 'test_conv_transpose_nn_grad', 'test_conj_op', 'test_complex_reshape', @@ -2529,7 +2529,7 @@ 'test_logical_op', 'test_imperative_deepcf', 'test_cholesky_op', - 'test_ir_fc_fuse_pass', + 'test_ir_fc_fuse_pass_deprecated', 'test_fleet_base_single', 'test_multiprocess_dataloader_iterable_dataset_dynamic', 'test_slice_op', @@ -2578,7 +2578,7 @@ 'test_seqconv_eltadd_relu_fuse_pass', 'test_analysis_predictor', 'test_convert_operators', - 'test_add_reader_dependency', + 'test_add_reader_dependency_deprecated', 'test_is_tensor', 'test_variable', 'test_save_model_without_var', @@ -2817,7 +2817,7 @@ 'test_sync_batch_norm_op', 'test_static_save_load', 'test_coalesce_tensor_op', - 'test_fuse_bn_act_pass', + 'test_fuse_bn_act_pass_deprecated', 'test_shard_index_op', 'test_cuda_random_seed', 'test_dequantize_log_op', @@ -2880,7 +2880,7 @@ 'test_tensor_register_hook', 'test_fused_multihead_matmul_op', 'test_uniform_random_inplace_op', - 'test_decoupled_py_reader', + 'test_decoupled_py_reader_deprecated', 'test_assign_op', 'test_trt_instance_norm_op', 'test_uniform_random_op', diff --git a/tools/static_mode_white_list.py b/tools/static_mode_white_list.py index e16c6c1fd494fe..f575e4ff223ecd 100755 --- a/tools/static_mode_white_list.py +++ b/tools/static_mode_white_list.py @@ -48,7 +48,7 @@ 'test_adaptive_avg_pool1d', 'test_adaptive_max_pool1d', 'test_add_position_encoding_op', - 'test_add_reader_dependency', + 'test_add_reader_dependency_deprecated', 'test_addmm_op', 'test_affine_grid_op', 'test_allclose_layer', @@ -123,11 +123,11 @@ 'test_cvm_op', 'test_data', 'test_dataloader_early_reset', - 'test_dataloader_keep_order', - 'test_dataloader_unkeep_order', + 'test_dataloader_keep_order_deprecated', + 'test_dataloader_unkeep_order_deprecated', 'test_debugger', 'test_decayed_adagrad_op', - 'test_decoupled_py_reader', + 'test_decoupled_py_reader_deprecated', 'test_decoupled_py_reader_data_check', 'test_deformable_conv_v1_op', 'test_deformable_psroi_pooling', @@ -500,7 +500,7 @@ 'test_communicator_sync', 'test_collective_optimizer', 'test_data_norm_op', - 'test_fuse_bn_act_pass', + 'test_fuse_bn_act_pass_deprecated', 'test_layers', 'test_sequence_conv', 'test_sequence_erase_op', @@ -518,7 +518,7 @@ 'test_sequence_topk_avg_pooling', 'test_sequence_unpad_op', 'test_ir_embedding_eltwise_layernorm_fuse_pass', - 'test_ir_fc_fuse_pass', + 'test_ir_fc_fuse_pass_deprecated', 'test_ir_skip_layernorm_pass', 'test_conv_bias_mkldnn_fuse_pass', 'test_conv_bn_fuse_pass', diff --git a/tools/windows/run_unittests.sh b/tools/windows/run_unittests.sh index 52ce0a10dfce38..b09345e094bacc 100644 --- a/tools/windows/run_unittests.sh +++ b/tools/windows/run_unittests.sh @@ -17,10 +17,10 @@ # TODO: fix these unittest that is bound to fail disable_wingpu_test="^test_model$|\ ^test_dataloader_early_reset$|\ -^test_add_reader_dependency$|\ +^test_add_reader_dependency_deprecated$|\ ^test_add_reader_dependency_for_interpretercore$|\ -^test_decoupled_py_reader$|\ -^test_decoupled_py_reader_static_build$|\ +^test_decoupled_py_reader_deprecated$|\ +^test_decoupled_py_reader_deprecated_static_build$|\ ^test_generator_dataloader_deprecated$|\ ^test_parallel_dygraph_sync_batch_norm$|\ ^test_py_reader_using_executor$|\ @@ -35,11 +35,11 @@ disable_wingpu_test="^test_model$|\ ^test_imperative_se_resnext$|\ ^test_sync_batch_norm_op$|\ ^test_sync_batch_norm_op_static_build$|\ -^test_dataloader_keep_order$|\ -^test_dataloader_unkeep_order$|\ +^test_dataloader_keep_order_deprecated$|\ +^test_dataloader_unkeep_order_deprecated$|\ ^test_multiprocess_dataloader_iterable_dataset_static$|\ -^test_fuse_bn_act_pass$|\ -^test_fuse_bn_act_pass_static_build$|\ +^test_fuse_bn_act_pass_deprecated$|\ +^test_fuse_bn_act_pass_deprecated_static_build$|\ ^test_fuse_bn_add_act_pass$|\ ^test_gather_op$|\ ^test_activation_op$|\ @@ -219,24 +219,24 @@ disable_wingpu_cuda12_test="^test_cholesky_op$|\ ^test_gru_unit_op$|\ ^test_matmul_op$|\ ^test_decoupled_py_reader_data_check$|\ -^test_decoupled_py_reader$|\ +^test_decoupled_py_reader_deprecated$|\ ^test_generator_dataloader_deprecated$|\ ^test_py_reader_combination$|\ ^test_reader_reset$|\ ^test_sync_batch_norm_op$|\ ^test_sync_batch_norm_op_static_build$|\ -^test_decoupled_py_reader_static_build$|\ +^test_decoupled_py_reader_deprecated_static_build$|\ ^test_multiprocess_dataloader_iterable_dataset_dynamic$|\ ^test_multiprocess_dataloader_iterable_dataset_static$|\ -^test_dataloader_keep_order$|\ -^test_dataloader_unkeep_order$|\ -^test_add_reader_dependency$|\ -^test_fuse_bn_act_pass$|\ -^test_fuse_bn_act_pass_static_build$|\ +^test_dataloader_keep_order_deprecated$|\ +^test_dataloader_unkeep_order_deprecated$|\ +^test_add_reader_dependency_deprecated$|\ +^test_fuse_bn_act_pass_deprecated$|\ +^test_fuse_bn_act_pass_deprecated_static_build$|\ ^test_fuse_bn_add_act_pass$|\ ^test_model$|\ ^test_dataloader_early_reset$|\ -^test_add_reader_dependency$|\ +^test_add_reader_dependency_deprecated$|\ ^test_conv2d_fusion_op$|\ ^test_fused_conv2d_add_act_op$|\ ^test_analyzer_detect_functional_mkldnn$|\ @@ -350,7 +350,7 @@ disable_win_inference_test="^trt_quant_int8_yolov3_r50_test$|\ ^test_conv_eltwiseadd_bn_fuse_pass$|\ ^test_custom_relu_op_setup$|\ ^test_conv3d_transpose_part2_op$|\ -^test_deform_conv2d$|\ +^test_deform_conv2d_deprecated$|\ ^test_matmul_op$|\ ^test_matmul_op_static_build$|\ ^test_basic_api_transformation$|\ @@ -370,7 +370,7 @@ disable_win_inference_test="^trt_quant_int8_yolov3_r50_test$|\ ^test_graph_khop_sampler$|\ ^test_gru_rnn_op$|\ ^test_masked_select_op$|\ -^test_ir_fc_fuse_pass$|\ +^test_ir_fc_fuse_pass_deprecated$|\ ^test_fc_elementwise_layernorm_fuse_pass$|\ ^test_linalg_pinv_op$|\ ^test_math_op_patch_var_base$|\ @@ -395,19 +395,19 @@ disable_win_inference_test="^trt_quant_int8_yolov3_r50_test$|\ ^test_py_reader_pin_memory$|\ ^test_multiprocess_dataloader_iterable_dataset_dynamic$|\ ^test_multiprocess_dataloader_iterable_dataset_static$|\ -^test_add_reader_dependency$|\ +^test_add_reader_dependency_deprecated$|\ ^test_add_reader_dependency_for_interpretercore$|\ ^test_compat$|\ -^test_decoupled_py_reader$|\ -^test_decoupled_py_reader_static_build$|\ +^test_decoupled_py_reader_deprecated$|\ +^test_decoupled_py_reader_deprecated_static_build$|\ ^test_generator_dataloader_deprecated$|\ ^test_py_reader_using_executor$|\ -^test_dataloader_keep_order$|\ -^test_dataloader_unkeep_order$|\ +^test_dataloader_keep_order_deprecated$|\ +^test_dataloader_unkeep_order_deprecated$|\ ^test_sync_batch_norm_op$|\ ^test_sync_batch_norm_op_static_build$|\ -^test_fuse_bn_act_pass$|\ -^test_fuse_bn_act_pass_static_build$|\ +^test_fuse_bn_act_pass_deprecated$|\ +^test_fuse_bn_act_pass_deprecated_static_build$|\ ^test_fuse_bn_add_act_pass$|\ ^test_decoupled_py_reader_data_check$|\ ^test_parallel_dygraph_sync_batch_norm$|\ From f60f3032f0f46a27f34f4033feb58c3f24533c77 Mon Sep 17 00:00:00 2001 From: Wang Huan Date: Wed, 5 Jun 2024 03:25:06 +0000 Subject: [PATCH 17/20] refine --- test/contrib/test_image_classification_fp16.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/contrib/test_image_classification_fp16.py b/test/contrib/test_image_classification_fp16.py index 4ba914616e2ab6..d927b68eff9b34 100644 --- a/test/contrib/test_image_classification_fp16.py +++ b/test/contrib/test_image_classification_fp16.py @@ -23,7 +23,7 @@ import numpy # TODO: remove sys.path.append -sys.path.append("../../legacy_test") +sys.path.append("../legacy_test") import nets import paddle From 5fdf3a21ba2715c556ac4da339c3d20137e906e8 Mon Sep 17 00:00:00 2001 From: Wang Huan Date: Wed, 5 Jun 2024 06:17:00 +0000 Subject: [PATCH 18/20] refine --- tools/parallel_UT_rule.py | 6 ++++-- tools/static_mode_white_list.py | 2 +- tools/windows/run_unittests.sh | 7 ++++--- 3 files changed, 9 insertions(+), 6 deletions(-) diff --git a/tools/parallel_UT_rule.py b/tools/parallel_UT_rule.py index 503209472304d7..23fae5e53097df 100755 --- a/tools/parallel_UT_rule.py +++ b/tools/parallel_UT_rule.py @@ -851,7 +851,7 @@ 'test_distribution', 'test_box_clip_op', 'custom_tensor_test', - 'test_dataloader_early_reset', + 'test_dataloader_early_reset_deprecated', 'test_gather_nd_op', 'test_tensor_register_hook', 'test_retain_graph', @@ -1332,6 +1332,7 @@ 'test_error', 'test_callbacks', 'test_imperative_recurrent_usage', + 'test_deform_conv2d', 'test_deform_conv2d_deprecated', 'test_coalesce_tensor_op', 'test_tsm', @@ -2348,6 +2349,7 @@ 'test_diagonal_op', 'test_diagflat', 'test_determinant_op', + 'test_deform_conv2d', 'test_deform_conv2d_deprecated', 'test_conv_transpose_nn_grad', 'test_conj_op', @@ -2766,7 +2768,7 @@ 'test_dot_op', 'test_device', 'test_imperative_layer_apply', - 'test_dataloader_early_reset', + 'test_dataloader_early_reset_deprecated', 'test_imperative_selected_rows_to_lod_tensor', 'test_crop_op', 'test_linear_interp_v2_op', diff --git a/tools/static_mode_white_list.py b/tools/static_mode_white_list.py index f575e4ff223ecd..f63bc17488e774 100755 --- a/tools/static_mode_white_list.py +++ b/tools/static_mode_white_list.py @@ -122,7 +122,7 @@ 'test_cumsum_op', 'test_cvm_op', 'test_data', - 'test_dataloader_early_reset', + 'test_dataloader_early_reset_deprecated', 'test_dataloader_keep_order_deprecated', 'test_dataloader_unkeep_order_deprecated', 'test_debugger', diff --git a/tools/windows/run_unittests.sh b/tools/windows/run_unittests.sh index b09345e094bacc..5d259e101b56d9 100644 --- a/tools/windows/run_unittests.sh +++ b/tools/windows/run_unittests.sh @@ -16,7 +16,7 @@ # /*================Fixed Disabled Windows CUDA10.x MKL(PR-CI-Windows) unittests===========================*/ # TODO: fix these unittest that is bound to fail disable_wingpu_test="^test_model$|\ -^test_dataloader_early_reset$|\ +^test_dataloader_early_reset_deprecated$|\ ^test_add_reader_dependency_deprecated$|\ ^test_add_reader_dependency_for_interpretercore$|\ ^test_decoupled_py_reader_deprecated$|\ @@ -235,7 +235,7 @@ disable_wingpu_cuda12_test="^test_cholesky_op$|\ ^test_fuse_bn_act_pass_deprecated_static_build$|\ ^test_fuse_bn_add_act_pass$|\ ^test_model$|\ -^test_dataloader_early_reset$|\ +^test_dataloader_early_reset_deprecated$|\ ^test_add_reader_dependency_deprecated$|\ ^test_conv2d_fusion_op$|\ ^test_fused_conv2d_add_act_op$|\ @@ -350,6 +350,7 @@ disable_win_inference_test="^trt_quant_int8_yolov3_r50_test$|\ ^test_conv_eltwiseadd_bn_fuse_pass$|\ ^test_custom_relu_op_setup$|\ ^test_conv3d_transpose_part2_op$|\ +^test_deform_conv2d$|\ ^test_deform_conv2d_deprecated$|\ ^test_matmul_op$|\ ^test_matmul_op_static_build$|\ @@ -411,7 +412,7 @@ disable_win_inference_test="^trt_quant_int8_yolov3_r50_test$|\ ^test_fuse_bn_add_act_pass$|\ ^test_decoupled_py_reader_data_check$|\ ^test_parallel_dygraph_sync_batch_norm$|\ -^test_dataloader_early_reset$|\ +^test_dataloader_early_reset_deprecated$|\ ^test_fleet_base_single$|\ ^test_sequence_pool$|\ ^test_simplify_with_basic_ops_pass_autoscan$|\ From 8aa8622f62d9be79116b5b3bba63850660292613 Mon Sep 17 00:00:00 2001 From: Wang Huan Date: Wed, 5 Jun 2024 11:05:21 +0000 Subject: [PATCH 19/20] refine --- ...st_comp_skip_op_set_deprecated.py => test_comp_skip_op_set.py} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename test/deprecated/prim/{test_comp_skip_op_set_deprecated.py => test_comp_skip_op_set.py} (100%) diff --git a/test/deprecated/prim/test_comp_skip_op_set_deprecated.py b/test/deprecated/prim/test_comp_skip_op_set.py similarity index 100% rename from test/deprecated/prim/test_comp_skip_op_set_deprecated.py rename to test/deprecated/prim/test_comp_skip_op_set.py From 01e5b6ab5b4776783337a386c00464121bec4bda Mon Sep 17 00:00:00 2001 From: Wang Huan Date: Thu, 6 Jun 2024 03:30:27 +0000 Subject: [PATCH 20/20] refine --- test/deprecated/tokenizer/CMakeLists.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/test/deprecated/tokenizer/CMakeLists.txt b/test/deprecated/tokenizer/CMakeLists.txt index 674bcb192241e9..cbab1a270c28f4 100644 --- a/test/deprecated/tokenizer/CMakeLists.txt +++ b/test/deprecated/tokenizer/CMakeLists.txt @@ -10,3 +10,4 @@ endforeach() set_tests_properties(test_faster_tokenizer_op_deprecated PROPERTIES LABELS "RUN_TYPE=EXCLUSIVE") +set_tests_properties(test_faster_tokenizer_op_deprecated PROPERTIES TIMEOUT 120)