Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
vladimir-paramuzov committed Jul 22, 2024
1 parent ec9ec6c commit dda3fd0
Show file tree
Hide file tree
Showing 15 changed files with 234 additions and 348 deletions.
20 changes: 5 additions & 15 deletions src/plugins/intel_gpu/src/graph/graph_optimizer/handle_reshape.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,23 +113,13 @@ void handle_reshape::run(program& p) {
// Copy reorder_node_to_split to iteration
std::vector<program_node*> reorder_users(reorder_node_to_split);
for (const auto& reorder_node : reorder_users) {
auto output_data_type = reorder_node->get_output_layout().data_type;
bool onednn_support = true;
for (const auto& user : onednn_users) {
auto out_dt = user->get_output_layout().data_type;
if (user->is_type<fully_connected>() || user->is_type<gemm>()) {
bool is_fc = user->is_type<fully_connected>();
auto wei_dt = is_fc ? user->as<fully_connected>().weights().get_output_layout().data_type :
user->as<gemm>().get_input_layout(1).data_type;
onednn_support = layout_optimizer::onednn_check_data_types_for_fc_gemm(output_data_type, wei_dt, out_dt);
} else if (user->is_type<convolution>() || user->is_type<deconvolution>()) {
bool is_conv = user->is_type<convolution>();
auto wei_dt = is_conv ? user->as<convolution>().weights().get_output_layout().data_type :
user->as<deconvolution>().weights().get_output_layout().data_type;
onednn_support = layout_optimizer::onednn_check_data_types_for_convolution(output_data_type, wei_dt, out_dt);
} else if (user->is_type<pooling>()) {
onednn_support = layout_optimizer::onednn_check_data_types_for_pooling(output_data_type, out_dt);
}

auto idx = user->get_dependency_index(*node);
user->replace_dependency(idx, *reorder_node, false);
onednn_support = user->can_use(impl_types::onednn);
user->replace_dependency(idx, *node, false);

if (!onednn_support) {
reorder_node_to_split.erase(std::remove(reorder_node_to_split.begin(), reorder_node_to_split.end(), reorder_node),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,19 @@ using namespace cldnn;
#define LOG_NODE_REMOVAL(id) GPU_DEBUG_LOG_PASS << __func__ << ":" << __LINE__ << ": remove node: " << (id) << std::endl;
#define LOG_NODE_REPLACEMENT(id) GPU_DEBUG_LOG_PASS << __func__ << ":" << __LINE__ << ": replace node: " << (id) << std::endl;

namespace {

bool does_any_user_have_impl_type(program_node& node, impl_types impl) {
for (auto& user : node.get_users()) {
if (user->get_preferred_impl_type() == impl)
return true;
}

return false;
}

} // namespace

remove_redundant_reorders::remove_redundant_reorders(layout_optimizer& lo_ref, bool enable_reorder_fusing, bool update_implementations,
bool remove_output_reorders)
: base_pass("remove_redundant_reorders"), lo(lo_ref), enable_reorder_fusing(enable_reorder_fusing), update_implementations(update_implementations),
Expand Down Expand Up @@ -289,7 +302,7 @@ void remove_redundant_reorders::run(program& p) {
i_layout.data_padding.upper_size().spatial[1] == 0 && i_layout.data_padding.lower_size().spatial[1] == 0 &&
o_layout.data_padding.upper_size() == (tensor)0 && o_layout.data_padding.lower_size() == (tensor)0 &&
i_layout.data_type == o_layout.data_type &&
!layout_optimizer::onednn_check_preferred_impl_type_of_users(r_node)) {
!does_any_user_have_impl_type(r_node, impl_types::onednn)) {
// If the newly aligned pad is merged into output layout during post_optimize_graph phase
// and then buffer is reinterpreted, user node cannot handle pad properly for kernel execution
if (!update_implementations || (i_layout.feature() % 16 == 0 &&
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,6 @@ void select_preferred_formats::run(program& p) {
}) != forcing_map.end())
continue;


// Onednn primitive descriptor creation may fail, for example, due to asymmetric weight.
try {
n->select_preferred_formats(impl_types::onednn);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,23 +112,6 @@ struct concatenation_onednn : typed_primitive_onednn_impl<concatenation, dnnl::c
#endif
}

static bool validate(const concatenation_node& node) {
if (one_of(node.get_output_layout().data_type, {data_types::i32, data_types::f32}))
return false;

for (auto& dep : node.get_dependencies()) {
if (dep.first->is_in_data_flow() && dep.first->get_preferred_impl_type() == impl_types::onednn) {
return false;
}
}

if (format::is_blocked(node.get_output_layout().format)) {
return false;
}

return true;
}

static std::unique_ptr<primitive_impl> create(const concatenation_node& arg, const kernel_impl_params& impl_params) {
auto& engine = impl_params.prog->get_engine();
auto& config = impl_params.prog->get_config();
Expand All @@ -150,7 +133,20 @@ struct concatenation_factory : public cldnn::implementation_factory<concatenatio

bool validate(const program_node& node) const override {
OPENVINO_ASSERT(node.is_type<concatenation>());
return onednn::concatenation_onednn::validate(static_cast<const concatenation_node&>(node));
if (one_of(node.get_output_layout().data_type, {data_types::i32, data_types::f32}))
return false;

for (auto& dep : node.get_dependencies()) {
if (dep.first->is_in_data_flow() && dep.first->get_preferred_impl_type() == impl_types::onednn) {
return false;
}
}

if (format::is_blocked(node.get_output_layout().format)) {
return false;
}

return true;
}

in_out_fmts_t query_formats(const program_node& node) const override {
Expand Down
55 changes: 24 additions & 31 deletions src/plugins/intel_gpu/src/graph/impls/onednn/convolution_onednn.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -334,33 +334,6 @@ struct convolution_onednn : typed_primitive_onednn_impl<convolution> {
#endif
}


static bool validate(const convolution_node& node) {
if (!is_supported_format(node.get_preferred_input_fmt(0)))
return false;

auto in_dt = node.get_input_layout(0).data_type;
auto wei_dt = node.weights().get_output_layout().data_type;
auto out_dt = node.get_output_layout(false).data_type;

bool f16_conv = everyone_is(data_types::f16, in_dt, wei_dt) && one_of(out_dt, {data_types::f16, data_types::f32, data_types::u8, data_types::i8});
bool u8s8_conv = one_of(in_dt, {data_types::i8, data_types::u8}) &&
wei_dt == data_types::i8 &&
one_of(out_dt, {data_types::i32, data_types::f16, data_types::f32, data_types::u8, data_types::i8});

if (!f16_conv && !u8s8_conv)
return false;

if (!is_supported_post_ops(node))
return false;

// oneDNN doesn't support asymmetric weights quantization
if (node.weights_zero_points_term())
return false;

return true;
}

static std::unique_ptr<primitive_impl> create(const convolution_node& arg, const kernel_impl_params& impl_params) {
auto& engine = impl_params.prog->get_engine();
auto& config = impl_params.prog->get_config();
Expand All @@ -384,7 +357,30 @@ struct convolution_factory : public cldnn::implementation_factory<convolution> {

bool validate(const program_node& node) const override {
OPENVINO_ASSERT(node.is_type<convolution>());
return convolution_onednn::validate(static_cast<const convolution_node&>(node));
const auto& conv_node = node.as<convolution>();
if (!is_supported_format(node.get_preferred_input_fmt(0)))
return false;

auto in_dt = conv_node.get_input_layout(0).data_type;
auto wei_dt = conv_node.weights().get_output_layout().data_type;
auto out_dt = conv_node.get_output_layout(false).data_type;

bool f16_conv = everyone_is(data_types::f16, in_dt, wei_dt) && one_of(out_dt, {data_types::f16, data_types::f32, data_types::u8, data_types::i8});
bool u8s8_conv = one_of(in_dt, {data_types::i8, data_types::u8}) &&
wei_dt == data_types::i8 &&
one_of(out_dt, {data_types::i32, data_types::f16, data_types::f32, data_types::u8, data_types::i8});

if (!f16_conv && !u8s8_conv)
return false;

if (!is_supported_post_ops(conv_node))
return false;

// oneDNN doesn't support asymmetric weights quantization
if (conv_node.weights_zero_points_term())
return false;

return true;
}

in_out_fmts_t query_formats(const program_node& node) const override {
Expand Down Expand Up @@ -454,9 +450,6 @@ struct convolution_factory : public cldnn::implementation_factory<convolution> {
if (out_fmts[0] == format::any) {
out_fmts[0] = dst_fmt;
}

GPU_DEBUG_LOG << "select_preferred_formats:" << node.id() << ": " << fmt_to_str(src_fmt) << " --> " << fmt_to_str(dst_fmt)
<< " For index : " << idx << std::endl;
}
return {in_fmts, out_fmts};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
//

#include "deconvolution_inst.h"
#include "impls/onednn/register.hpp"
#include "impls/onednn/utils.hpp"
#include "intel_gpu/runtime/utils.hpp"
#include "primitive_onednn_base.h"
Expand All @@ -13,7 +12,6 @@

#include <memory>
namespace cldnn {

namespace onednn {

static std::shared_ptr<dnnl::deconvolution_forward::primitive_desc> get_deconvolution_primitive_descriptor(const kernel_impl_params& impl_params,
Expand Down Expand Up @@ -204,16 +202,34 @@ struct deconvolution_onednn : typed_primitive_onednn_impl<deconvolution> {
#endif
}

static bool validate(const deconvolution_node& node) {
static std::unique_ptr<primitive_impl> create(const deconvolution_node& arg, const kernel_impl_params& impl_params) {
auto& engine = impl_params.prog->get_engine();
auto& config = impl_params.prog->get_config();
auto attr = impl_params.attrs_onednn;
auto prim_desc = get_deconvolution_primitive_descriptor(impl_params, *attr);

return cldnn::make_unique<deconvolution_onednn>(engine, config, attr, *prim_desc, get_weights_reorder(impl_params, *prim_desc));
}
};

struct deconvolution_factory : public cldnn::implementation_factory<deconvolution> {
std::unique_ptr<primitive_impl> create(const program_node& node, const kernel_impl_params& params) const override {
OPENVINO_ASSERT(node.is_type<deconvolution>());
return onednn::deconvolution_onednn::create(static_cast<const deconvolution_node&>(node), params);
}

bool validate(const program_node& node) const override {
OPENVINO_ASSERT(node.is_type<deconvolution>());
const auto& deconv_node = node.as<deconvolution>();
if (!is_supported_format(node.get_preferred_input_fmt(0)))
return false;

const auto& input_layout = node.get_input_layout(0);
const auto& input_layout = deconv_node.get_input_layout(0);
auto in_dt = input_layout.data_type;
auto wei_dt = node.weights().get_output_layout().data_type;
auto out_dt = node.get_output_layout(false).data_type;
auto wei_dt = deconv_node.weights().get_output_layout().data_type;
auto out_dt = deconv_node.get_output_layout(false).data_type;

const auto& prim = node.get_primitive();
const auto& prim = deconv_node.get_primitive();

if (prim->groups != 1)
return false;
Expand All @@ -232,33 +248,12 @@ struct deconvolution_onednn : typed_primitive_onednn_impl<deconvolution> {
if (!f16_deconv && !f32_deconv && !u8s8_deconv)
return false;

if (!is_supported_post_ops(node))
if (!is_supported_post_ops(deconv_node))
return false;

return true;
}

static std::unique_ptr<primitive_impl> create(const deconvolution_node& arg, const kernel_impl_params& impl_params) {
auto& engine = impl_params.prog->get_engine();
auto& config = impl_params.prog->get_config();
auto attr = impl_params.attrs_onednn;
auto prim_desc = get_deconvolution_primitive_descriptor(impl_params, *attr);

return cldnn::make_unique<deconvolution_onednn>(engine, config, attr, *prim_desc, get_weights_reorder(impl_params, *prim_desc));
}
};

struct deconvolution_factory : public cldnn::implementation_factory<deconvolution> {
std::unique_ptr<primitive_impl> create(const program_node& node, const kernel_impl_params& params) const override {
OPENVINO_ASSERT(node.is_type<deconvolution>());
return onednn::deconvolution_onednn::create(static_cast<const deconvolution_node&>(node), params);
}

bool validate(const program_node& node) const override {
OPENVINO_ASSERT(node.is_type<deconvolution>());
return onednn::deconvolution_onednn::validate(static_cast<const deconvolution_node&>(node));
}

in_out_fmts_t query_formats(const program_node& node) const override {
OPENVINO_ASSERT(node.is_type<deconvolution>());
std::vector<format::type> in_fmts(node.get_dependencies().size(), format::any);
Expand All @@ -284,16 +279,10 @@ struct deconvolution_factory : public cldnn::implementation_factory<deconvolutio

in_fmts[idx] = src_fmt;

auto dst_fmt = onednn::find_data_format(prim_desc->dst_desc());

if (out_fmts[0] == format::any) {
out_fmts[0] = dst_fmt;
}

GPU_DEBUG_LOG << "select_preferred_formats:" << node.id() << ": " << fmt_to_str(src_fmt) << " --> " << fmt_to_str(dst_fmt)
<< " For index : " << idx << std::endl;
}

out_fmts[0] = onednn::find_data_format(prim_desc->dst_desc());

return {in_fmts, out_fmts};
}
};
Expand Down Expand Up @@ -322,7 +311,6 @@ attach_deconvolution_onednn::attach_deconvolution_onednn() {
format::bs_fs_yx_bsv8_fsv2,
format::bs_fs_yx_bsv4_fsv2,
};

implementation_map<deconvolution>::add(impl_types::onednn, shape_types::static_shape, cldnn::make_unique<deconvolution_factory>(), dt, fmt);
}

Expand Down
Loading

0 comments on commit dda3fd0

Please sign in to comment.