From d233d75f606693e40b11a074c8f932a0aa120a35 Mon Sep 17 00:00:00 2001 From: fishbell Date: Wed, 25 Dec 2024 09:21:37 +0800 Subject: [PATCH] use random values in test Signed-off-by: fishbell --- .../graph_optimizer/reshape_transfer.cpp | 26 ++--- .../unit/passes/reorder_reshape_permute.cpp | 105 ++++++++---------- 2 files changed, 59 insertions(+), 72 deletions(-) diff --git a/src/plugins/intel_gpu/src/graph/graph_optimizer/reshape_transfer.cpp b/src/plugins/intel_gpu/src/graph/graph_optimizer/reshape_transfer.cpp index 90e0e351097a4f..90faed7160310a 100644 --- a/src/plugins/intel_gpu/src/graph/graph_optimizer/reshape_transfer.cpp +++ b/src/plugins/intel_gpu/src/graph/graph_optimizer/reshape_transfer.cpp @@ -34,22 +34,18 @@ void reshape_transfer::run(program& p) { is_suitable_parent = [&is_suitable_parent](const cldnn::program_node* node) -> bool { if (node->get_users().size() != 1 || node->is_dynamic()) return false; - if (node->is_type()) - return true; - if (node->is_type()) { - for (size_t idx = 0; idx < node->get_dependencies().size(); idx++) { - auto& input = node->get_dependency(idx); - if (!input.is_in_data_flow() || input.is_constant()) - continue; - if (input.is_type()) { - return true; - } else if (input.is_type() && input.get_dependency(1).is_constant()) { - return is_suitable_parent(&input); - } else if (input.is_type()) { - return is_suitable_parent(&input); - } - return false; + for (size_t idx = 0; idx < node->get_dependencies().size(); idx++) { + auto& input = node->get_dependency(idx); + if (!input.is_in_data_flow() || input.is_constant()) + continue; + if (node->is_type() || input.is_type()) { + return true; + } else if (input.is_type() && input.get_dependency(1).is_constant()) { + return is_suitable_parent(&input); + } else if (input.is_type()) { + return is_suitable_parent(&input); } + return false; } return false; }; diff --git a/src/plugins/intel_gpu/tests/unit/passes/reorder_reshape_permute.cpp b/src/plugins/intel_gpu/tests/unit/passes/reorder_reshape_permute.cpp index 7189b3e29ee978..6dfb067f18ea26 100644 --- a/src/plugins/intel_gpu/tests/unit/passes/reorder_reshape_permute.cpp +++ b/src/plugins/intel_gpu/tests/unit/passes/reorder_reshape_permute.cpp @@ -17,13 +17,9 @@ TEST(opt_reorder_reshape_permute, no_reshape) { auto in_layout = layout{ov::PartialShape({1, 2, 4, 6}), data_types::f16, format::bfyx}; auto input = engine.allocate_memory(layout{ov::PartialShape({1, 2, 4, 6}), data_types::f16, format::bfyx}); auto weight = engine.allocate_memory(layout{ov::PartialShape({3, 2, 1, 1}), data_types::f16, format::bfyx}); + tests::set_random_values(input); + tests::set_random_values(weight); - set_values(input, {2.0f, 3.0f, 4.0f, 4.0f, 3.0f, 2.0f, 1.f, 2.f, 3.f, 1.f, 2.f, 4.f, - 5.f, 1.f, 1.f, 2.f, 1.f, 2.f, 2.0f, 3.0f, 1.0f, 4.0f, 1.0f, 4.0f, - 3.0f, 2.0f, 0.0f, 1.0f, 0.0f, 2.0f, 2.f, 4.f, 1.f, 1.f, 2.f, 1.f, - 1.f, 2.f, 0.f, 2.f, 5.f, 2.f, 4.0f, 3.0f, 1.0f, 0.0f, 3.0f, 2.0f}); - - set_values(weight, {1.f, 1.f, 1.f, 1.f, 1.f, 1.f}); topology topology; topology.add(input_layout("input", in_layout)); topology.add(data("weight", weight)); @@ -62,9 +58,13 @@ TEST(opt_reorder_reshape_permute, no_reshape) { auto ref_output = ref_network.execute(); auto ref_out_mem = ref_output.at("softmax").get_memory(); mem_lock lock_ref(ref_out_mem, get_test_stream()); + auto tolerance = default_tolerance(ref_out_mem->get_layout().data_type); for (size_t i = 0; i < out_mem->count(); i++) { - float actual = lock[i]; - ASSERT_EQ(actual, lock_ref[i]); + ASSERT_NEAR(lock[i],lock_ref[i], tolerance) + << "\ntolerance = " << tolerance + << "\ni = " << i + << "\nref[i] = " << lock_ref[i] + << "\nopt[i] = " << lock[i]; } } @@ -73,12 +73,8 @@ TEST(opt_reorder_reshape_permute, no_reorder) { auto in_layout = layout{ov::PartialShape({1, 2, 4, 6}), data_types::f16, format::bfyx}; auto input = engine.allocate_memory(layout{ov::PartialShape({1, 2, 4, 6}), data_types::f16, format::bfyx}); auto weight = engine.allocate_memory(layout{ov::PartialShape({3, 2, 1, 1}), data_types::f16, format::bfyx}); - set_values(input, {2.0f, 3.0f, 4.0f, 4.0f, 3.0f, 2.0f, 1.f, 2.f, 3.f, 1.f, 2.f, 4.f, - 5.f, 1.f, 1.f, 2.f, 1.f, 2.f, 2.0f, 3.0f, 1.0f, 4.0f, 1.0f, 4.0f, - 3.0f, 2.0f, 0.0f, 1.0f, 0.0f, 2.0f, 2.f, 4.f, 1.f, 1.f, 2.f, 1.f, - 1.f, 2.f, 0.f, 2.f, 5.f, 2.f, 4.0f, 3.0f, 1.0f, 0.0f, 3.0f, 2.0f}); - - set_values(weight, {1.f, 1.f, 1.f, 1.f, 1.f, 1.f}); + tests::set_random_values(input); + tests::set_random_values(weight); topology topology; topology.add(input_layout("input", in_layout)); topology.add(data("weight", weight)); @@ -118,9 +114,13 @@ TEST(opt_reorder_reshape_permute, no_reorder) { auto ref_output = ref_network.execute(); auto ref_out_mem = ref_output.at("softmax").get_memory(); mem_lock lock_ref(ref_out_mem, get_test_stream()); + auto tolerance = default_tolerance(ref_out_mem->get_layout().data_type); for (size_t i = 0; i < out_mem->count(); i++) { - float actual = lock[i]; - ASSERT_EQ(actual, lock_ref[i]); + ASSERT_NEAR(lock[i],lock_ref[i], tolerance) + << "\ntolerance = " << tolerance + << "\ni = " << i + << "\nref[i] = " << lock_ref[i] + << "\nopt[i] = " << lock[i]; } } @@ -129,12 +129,8 @@ TEST(opt_reorder_reshape_permute, no_reorder_no_reshape) { auto in_layout = layout{ov::PartialShape({1, 2, 4, 6}), data_types::f16, format::bfyx}; auto input = engine.allocate_memory(layout{ov::PartialShape({1, 2, 4, 6}), data_types::f16, format::bfyx}); auto weight = engine.allocate_memory(layout{ov::PartialShape({3, 2, 1, 1}), data_types::f16, format::bfyx}); - set_values(input, {2.0f, 3.0f, 4.0f, 4.0f, 3.0f, 2.0f, 1.f, 2.f, 3.f, 1.f, 2.f, 4.f, - 5.f, 1.f, 1.f, 2.f, 1.f, 2.f, 2.0f, 3.0f, 1.0f, 4.0f, 1.0f, 4.0f, - 3.0f, 2.0f, 0.0f, 1.0f, 0.0f, 2.0f, 2.f, 4.f, 1.f, 1.f, 2.f, 1.f, - 1.f, 2.f, 0.f, 2.f, 5.f, 2.f, 4.0f, 3.0f, 1.0f, 0.0f, 3.0f, 2.0f}); - - set_values(weight, {1.f, 1.f, 1.f, 1.f, 1.f, 1.f}); + tests::set_random_values(input); + tests::set_random_values(weight); topology topology; topology.add(input_layout("input", in_layout)); topology.add(data("weight", weight)); @@ -165,9 +161,13 @@ TEST(opt_reorder_reshape_permute, no_reorder_no_reshape) { auto ref_output = ref_network.execute(); auto ref_out_mem = ref_output.at("softmax").get_memory(); mem_lock lock_ref(ref_out_mem, get_test_stream()); + auto tolerance = default_tolerance(ref_out_mem->get_layout().data_type); for (size_t i = 0; i < out_mem->count(); i++) { - float actual = lock[i]; - ASSERT_EQ(actual, lock_ref[i]); + ASSERT_NEAR(lock[i],lock_ref[i], tolerance) + << "\ntolerance = " << tolerance + << "\ni = " << i + << "\nref[i] = " << lock_ref[i] + << "\nopt[i] = " << lock[i]; } } @@ -176,12 +176,8 @@ TEST(opt_reorder_reshape_permute, cutomized_net_yolov6_alike) { auto in_layout = layout{ov::PartialShape({1, 2, 4, 6}), data_types::f16, format::bfyx}; auto input = engine.allocate_memory(layout{ov::PartialShape({1, 2, 4, 6}), data_types::f16, format::bfyx}); auto weight = engine.allocate_memory(layout{ov::PartialShape({3, 2, 1, 1}), data_types::f16, format::bfyx}); - set_values(input, {2.0f, 3.0f, 4.0f, 4.0f, 3.0f, 2.0f, 1.f, 2.f, 3.f, 1.f, 2.f, 4.f, - 5.f, 1.f, 1.f, 2.f, 1.f, 2.f, 2.0f, 3.0f, 1.0f, 4.0f, 1.0f, 4.0f, - 3.0f, 2.0f, 0.0f, 1.0f, 0.0f, 2.0f, 2.f, 4.f, 1.f, 1.f, 2.f, 1.f, - 1.f, 2.f, 0.f, 2.f, 5.f, 2.f, 4.0f, 3.0f, 1.0f, 0.0f, 3.0f, 2.0f}); - - set_values(weight, {1.f, 1.f, 1.f, 1.f, 1.f, 1.f}); + tests::set_random_values(input); + tests::set_random_values(weight); topology topology; topology.add(input_layout("input", in_layout)); topology.add(data("weight", weight)); @@ -231,9 +227,13 @@ TEST(opt_reorder_reshape_permute, cutomized_net_yolov6_alike) { auto ref_out_mem = ref_output.at("softmax").get_memory(); mem_lock lock_ref(ref_out_mem, get_test_stream()); + auto tolerance = default_tolerance(ref_out_mem->get_layout().data_type); for (size_t i = 0; i < out_mem->count(); i++) { - float actual = lock[i]; - ASSERT_EQ(actual, lock_ref[i]); + ASSERT_NEAR(lock[i],lock_ref[i], tolerance) + << "\ntolerance = " << tolerance + << "\ni = " << i + << "\nref[i] = " << lock_ref[i] + << "\nopt[i] = " << lock[i]; } } @@ -242,12 +242,8 @@ TEST(opt_reorder_reshape_permute, cutomized_net_yolov6_alike_4d) { auto in_layout = layout{ov::PartialShape({1, 2, 4, 6}), data_types::f16, format::bfyx}; auto input = engine.allocate_memory(layout{ov::PartialShape({1, 2, 4, 6}), data_types::f16, format::bfyx}); auto weight = engine.allocate_memory(layout{ov::PartialShape({3, 2, 1, 1}), data_types::f16, format::bfyx}); - set_values(input, {2.0f, 3.0f, 4.0f, 4.0f, 3.0f, 2.0f, 1.f, 2.f, 3.f, 1.f, 2.f, 4.f, - 5.f, 1.f, 1.f, 2.f, 1.f, 2.f, 2.0f, 3.0f, 1.0f, 4.0f, 1.0f, 4.0f, - 3.0f, 2.0f, 0.0f, 1.0f, 0.0f, 2.0f, 2.f, 4.f, 1.f, 1.f, 2.f, 1.f, - 1.f, 2.f, 0.f, 2.f, 5.f, 2.f, 4.0f, 3.0f, 1.0f, 0.0f, 3.0f, 2.0f}); - - set_values(weight, {1.f, 1.f, 1.f, 1.f, 1.f, 1.f}); + tests::set_random_values(input); + tests::set_random_values(weight); topology topology; topology.add(input_layout("input", in_layout)); topology.add(data("weight", weight)); @@ -297,9 +293,13 @@ TEST(opt_reorder_reshape_permute, cutomized_net_yolov6_alike_4d) { auto ref_out_mem = ref_output.at("softmax").get_memory(); mem_lock lock_ref(ref_out_mem, get_test_stream()); + auto tolerance = default_tolerance(ref_out_mem->get_layout().data_type); for (size_t i = 0; i < out_mem->count(); i++) { - float actual = lock[i]; - ASSERT_EQ(actual, lock_ref[i]); + ASSERT_NEAR(lock[i],lock_ref[i], tolerance) + << "\ntolerance = " << tolerance + << "\ni = " << i + << "\nref[i] = " << lock_ref[i] + << "\nopt[i] = " << lock[i]; } } @@ -308,12 +308,8 @@ TEST(opt_reorder_reshape_permute, not_sinking_reshape) { auto in_layout = layout{ov::PartialShape({1, 2, 4, 6}), data_types::f16, format::bfyx}; auto input = engine.allocate_memory(layout{ov::PartialShape({1, 2, 4, 6}), data_types::f16, format::bfyx}); auto weight = engine.allocate_memory(layout{ov::PartialShape({3, 2, 1, 1}), data_types::f16, format::bfyx}); - set_values(input, {2.0f, 3.0f, 4.0f, 4.0f, 3.0f, 2.0f, 1.f, 2.f, 3.f, 1.f, 2.f, 4.f, - 5.f, 1.f, 1.f, 2.f, 1.f, 2.f, 2.0f, 3.0f, 1.0f, 4.0f, 1.0f, 4.0f, - 3.0f, 2.0f, 0.0f, 1.0f, 0.0f, 2.0f, 2.f, 4.f, 1.f, 1.f, 2.f, 1.f, - 1.f, 2.f, 0.f, 2.f, 5.f, 2.f, 4.0f, 3.0f, 1.0f, 0.0f, 3.0f, 2.0f}); - - set_values(weight, {1.f, 1.f, 1.f, 1.f, 1.f, 1.f}); + tests::set_random_values(input); + tests::set_random_values(weight); topology topology; topology.add(input_layout("input", in_layout)); topology.add(data("weight", weight)); @@ -336,16 +332,8 @@ TEST(opt_reorder_reshape_permute, not_sinking_reshape) { net.set_input_data("input", input); auto output = net.execute(); - auto optimzed_nodes = net.get_program()->get_optimized(); - auto it = - std::find_if(std::begin(optimzed_nodes), std::end(optimzed_nodes), [&](cldnn::program::optimized_info& oi) { - return oi.first == "reorder_inter"; - }); - ASSERT_NE(it, optimzed_nodes.end()); auto permute_inst = net.get_primitive("permute_inter"); ASSERT_FALSE(permute_inst->can_be_optimized()); - auto reshape_inst = net.get_primitive("reshape_inter"); - ASSERT_FALSE(reshape_inst->can_be_optimized()); auto& processing_order = prog->get_processing_order(); @@ -363,9 +351,12 @@ TEST(opt_reorder_reshape_permute, not_sinking_reshape) { auto ref_out_mem = ref_output.at("softmax").get_memory(); mem_lock lock_ref(ref_out_mem, get_test_stream()); + auto tolerance = default_tolerance(ref_out_mem->get_layout().data_type); for (size_t i = 0; i < out_mem->count(); i++) { - float actual = lock[i]; - std::cout << actual << ", " << std::endl; - ASSERT_EQ(actual, lock_ref[i]); + ASSERT_NEAR(lock[i],lock_ref[i], tolerance) + << "\ntolerance = " << tolerance + << "\ni = " << i + << "\nref[i] = " << lock_ref[i] + << "\nopt[i] = " << lock[i]; } }