Skip to content

Commit

Permalink
relaxed pattern match to allow reorder not in place
Browse files Browse the repository at this point in the history
Signed-off-by: fishbell <[email protected]>
  • Loading branch information
songbell committed Dec 24, 2024
1 parent 58aafd5 commit 293a707
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,24 +30,28 @@ void reshape_transfer::run(program& p) {
return false;
};

std::function<bool(cldnn::program_node*)> is_suitable_reorder;
is_suitable_reorder = [&is_suitable_reorder](const cldnn::program_node* node) -> bool {
std::function<bool(cldnn::program_node*)> is_suitable_parent;
is_suitable_parent = [&is_suitable_parent](const cldnn::program_node* node) -> bool {
if (node->get_users().size() != 1 || node->is_dynamic())
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 (input.is_type<convolution>()) {
return true;
} else if (input.is_type<eltwise>() && input.get_dependency(1).is_constant()) {
return is_suitable_reorder(&input);
} else if (input.is_type<activation>()) {
return is_suitable_reorder(&input);
if (node->is_type<convolution>())
return true;
if (node->is_type<reorder>()) {
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<convolution>()) {
return true;
} else if (input.is_type<eltwise>() && input.get_dependency(1).is_constant()) {
return is_suitable_parent(&input);
} else if (input.is_type<activation>()) {
return is_suitable_parent(&input);
}
return false;
}
return false;
}
return true;
return false;
};

auto update_order = [](std::vector<uint16_t> original_order, cldnn::program_node* reshape) {
Expand All @@ -63,7 +67,7 @@ void reshape_transfer::run(program& p) {
auto transformed_order = original_order;
ov::Shape new_shape(transformed_order.size());
const uint16_t merge_dim_idx = [&]() {
for (size_t i = 0; i < reshape_in_shape.size(); ++i) {
for (uint16_t i = 0; i < reshape_in_shape.size(); ++i) {
if (reshape_in_shape[i] != reshape_out_shape[i])
return i;
}
Expand Down Expand Up @@ -110,7 +114,7 @@ void reshape_transfer::run(program& p) {
continue;
}

if (!is_suitable_reorder(parent_node)) {
if (!is_suitable_parent(parent_node)) {
continue;
}
reshape_node* reshape_node = nullptr;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,62 @@ TEST(opt_reorder_reshape_permute, no_reshape) {
}
}

TEST(opt_reorder_reshape_permute, no_reorder) {
auto& engine = get_test_engine();
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<ov::float16>(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<ov::float16>(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));
topology.add(
convolution("convolution", input_info("input"), "weight", "", 1, {1, 1}, {1, 1}, {0, 0}, {0, 0}, false));
topology.add(
reshape("reshape_inter", input_info("convolution"), false, {1, 3, 24, 1}, ov::PartialShape{1, 3, 24, 1}));
topology.add(permute("permute_inter", input_info("reshape_inter"), {0, 2, 1}));
topology.add(softmax("softmax", input_info("permute_inter"), 1));
ExecutionConfig config = get_test_default_config(engine);
config.set_property(ov::intel_gpu::allow_new_shape_infer(true));
config.set_property(ov::intel_gpu::optimize_data(true));
auto prog = program::build_program(engine, topology, config);

network net(prog);

net.set_input_data("input", input);
auto output = net.execute();

ExecutionConfig ref_config = get_test_default_config(engine);
ref_config.set_property(ov::intel_gpu::optimize_data(false));
cldnn::network ref_network(engine, topology, ref_config);
auto& processing_order = prog->get_processing_order();
auto reshape_node = std::find(processing_order.begin(), processing_order.end(), &prog->get_node("reshape_inter"));
size_t reshape_dist = std::distance(processing_order.begin(), reshape_node);

auto permute_node = std::find(processing_order.begin(), processing_order.end(), &prog->get_node("permute_inter"));
size_t permute_dist = std::distance(processing_order.begin(), permute_node);
ASSERT_TRUE(reshape_dist > permute_dist);
// select preferred formats, conv + permute
auto permute_inst = net.get_primitive("permute_inter");
ASSERT_TRUE(permute_inst->can_be_optimized());
auto out_mem = output.at("softmax").get_memory();
mem_lock<ov::float16> lock(out_mem, get_test_stream());

ref_network.set_input_data("input", input);
auto ref_output = ref_network.execute();
auto ref_out_mem = ref_output.at("softmax").get_memory();
mem_lock<ov::float16> lock_ref(ref_out_mem, get_test_stream());
for (size_t i = 0; i < out_mem->count(); i++) {
float actual = lock[i];
ASSERT_EQ(actual, lock_ref[i]);
}
}

TEST(opt_reorder_reshape_permute, no_reorder_no_reshape) {
auto& engine = get_test_engine();
auto in_layout = layout{ov::PartialShape({1, 2, 4, 6}), data_types::f16, format::bfyx};
Expand Down

0 comments on commit 293a707

Please sign in to comment.