From e34d1da5b538cd278c2523c24bf1474847016f73 Mon Sep 17 00:00:00 2001 From: Taylor Yeonbok Lee Date: Sat, 23 Jul 2022 17:12:54 +0900 Subject: [PATCH] Add and apply optional_layout --- .../include/intel_gpu/runtime/layout.hpp | 34 ++++++++++++ .../graph/impls/ocl/binary_convolution.cpp | 2 +- .../src/graph/impls/ocl/deconvolution.cpp | 4 -- .../graph/impls/onednn/convolution_onednn.cpp | 21 ++++---- .../impls/onednn/deconvolution_onednn.cpp | 6 +-- .../impls/onednn/fully_connected_onednn.cpp | 5 +- .../graph/include/binary_convolution_inst.h | 9 +--- .../src/graph/include/convolution_inst.h | 18 ++----- .../src/graph/include/deconvolution_inst.h | 4 +- .../include/deformable_convolution_inst.h | 5 +- .../src/graph/include/fully_connected_inst.h | 4 +- .../graph/include/kernel_selector_helper.h | 54 +++++++++---------- 12 files changed, 86 insertions(+), 80 deletions(-) diff --git a/src/plugins/intel_gpu/include/intel_gpu/runtime/layout.hpp b/src/plugins/intel_gpu/include/intel_gpu/runtime/layout.hpp index e6e740dcfdc5c9..bbf9d7c8de9227 100644 --- a/src/plugins/intel_gpu/include/intel_gpu/runtime/layout.hpp +++ b/src/plugins/intel_gpu/include/intel_gpu/runtime/layout.hpp @@ -68,6 +68,7 @@ class optional_data_type { storage_type storage; }; + /// Converts C++ type to @ref data_types . template struct type_to_data_type; @@ -528,6 +529,39 @@ struct layout { std::string to_string() const; }; +class optional_layout { +public: + optional_layout() {} + optional_layout(const layout& lay) { + this->opt_layout_ptr = make_unique(lay); + } + + optional_layout(const optional_layout& new_opt_lay) { + //this->opt_layout = std::move(*new_opt_lay); + if (new_opt_lay) { + layout copied_lay = *new_opt_lay; + this->opt_layout_ptr = make_unique(copied_lay); + } + } + + operator bool() const { + return this->opt_layout_ptr != nullptr; + } + + layout operator*() const { + if (opt_layout_ptr == nullptr) + throw std::runtime_error("Attempt to access uninitialized optional layout!"); + return *this->opt_layout_ptr; + } + + std::unique_ptr& get_layout_ptr() { + return opt_layout_ptr; + } + +private: + std::unique_ptr opt_layout_ptr = nullptr; +}; + /// @} /// @} } // namespace cldnn diff --git a/src/plugins/intel_gpu/src/graph/impls/ocl/binary_convolution.cpp b/src/plugins/intel_gpu/src/graph/impls/ocl/binary_convolution.cpp index b5c73876a571fe..26c0697a33a46a 100644 --- a/src/plugins/intel_gpu/src/graph/impls/ocl/binary_convolution.cpp +++ b/src/plugins/intel_gpu/src/graph/impls/ocl/binary_convolution.cpp @@ -62,7 +62,7 @@ struct binary_convolution_impl : typed_primitive_impl_ocl { public: static primitive_impl* create(const binary_convolution_node& arg, std::shared_ptr impl_param) { const auto& primitive = arg.get_primitive(); - const auto& weights_layout = impl_param->weights_layout.convert_to_weights_layout(false); + const auto& weights_layout = (*impl_param->weights_layout).convert_to_weights_layout(false); const auto& weights_size = weights_layout.size; const auto& split = primitive->split(); diff --git a/src/plugins/intel_gpu/src/graph/impls/ocl/deconvolution.cpp b/src/plugins/intel_gpu/src/graph/impls/ocl/deconvolution.cpp index b76b19aeed9ee1..189d098553afdd 100644 --- a/src/plugins/intel_gpu/src/graph/impls/ocl/deconvolution.cpp +++ b/src/plugins/intel_gpu/src/graph/impls/ocl/deconvolution.cpp @@ -55,11 +55,7 @@ struct deconvolution_impl : typed_primitive_impl_ocl { const auto& primitive = arg.get_primitive(); const auto& split = primitive->split(); const auto& stride = primitive->stride; -#if 0 // TODO: support dilation - const auto& dilation = primitive->dilation; -#else const ov::Strides dilation(impl_param->output_layout.get_spatial_rank(), 1); -#endif const auto actual_split = split; const auto& pad = primitive->pad; diff --git a/src/plugins/intel_gpu/src/graph/impls/onednn/convolution_onednn.cpp b/src/plugins/intel_gpu/src/graph/impls/onednn/convolution_onednn.cpp index dc5d83214a5f29..96033658edbc72 100644 --- a/src/plugins/intel_gpu/src/graph/impls/onednn/convolution_onednn.cpp +++ b/src/plugins/intel_gpu/src/graph/impls/onednn/convolution_onednn.cpp @@ -158,22 +158,19 @@ struct convolution_onednn : typed_primitive_onednn_implgrouped_weights_shape); - const auto& bias_layout = arg.bias_term() ? arg.bias().get_output_layout() : layout(data_types::f32, format::any, tensor()); - const auto& weights_zero_points_layout = arg.weights_zero_points_term() ? arg.weights_zero_points().get_output_layout() - : layout(data_types::f32, format::any, tensor()); - const auto& activations_zero_points_layout = arg.activations_zero_points_term() ? arg.activations_zero_points().get_output_layout() - : layout(data_types::f32, format::any, tensor()); - const auto& compensation_layout = arg.compensation_term() ? arg.compensation().get_output_layout() - : layout(data_types::f32, format::any, tensor()); - const auto& param_info = kernel_impl_params(arg.get_program(), cldnn_prim, arg.get_unique_id(), arg.get_input_layouts(), arg.get_output_layout(), arg.get_fused_primitives(), arg.get_fused_activations_funcs(), arg.get_fused_activations_params(), - arg.weights().get_output_layout(), arg.bias_term(), bias_layout, - arg.weights_zero_points_term(), weights_zero_points_layout, - arg.activations_zero_points_term(), activations_zero_points_layout, - arg.compensation_term(), compensation_layout); + optional_layout(weights_layout), + arg.bias_term() ? optional_layout(arg.bias().get_output_layout()) : optional_layout(), + arg.weights_zero_points_term() ? optional_layout(arg.weights_zero_points().get_output_layout()) + : optional_layout(), + arg.activations_zero_points_term() ? optional_layout(arg.activations_zero_points().get_output_layout()) + : optional_layout(), + arg.compensation_term() ? optional_layout(arg.compensation().get_output_layout()) + : optional_layout()); + set_params(param_info, r_params); r_params.layerID = arg.id() + "_reorder_"; r_params.input = convert_weights_tensor(weights_layout, cldnn_prim->grouped_weights_shape); diff --git a/src/plugins/intel_gpu/src/graph/impls/onednn/deconvolution_onednn.cpp b/src/plugins/intel_gpu/src/graph/impls/onednn/deconvolution_onednn.cpp index 98956c6b8494ce..82f08a0c94d2af 100644 --- a/src/plugins/intel_gpu/src/graph/impls/onednn/deconvolution_onednn.cpp +++ b/src/plugins/intel_gpu/src/graph/impls/onednn/deconvolution_onednn.cpp @@ -78,13 +78,13 @@ struct deconvolution_onednn : typed_primitive_onednn_implgrouped_weights_shape); - const auto& bias_layout = arg.bias_term() ? arg.bias().get_output_layout() : layout(data_types::f32, format::any, tensor()); - const auto& param_info = kernel_impl_params(arg.get_program(), cldnn_prim, arg.get_unique_id(), arg.get_input_layouts(), arg.get_output_layout(), arg.get_fused_primitives(), arg.get_fused_activations_funcs(), arg.get_fused_activations_params(), - arg.weights().get_output_layout(), arg.bias_term(), bias_layout); + optional_layout(weights_layout), + arg.bias_term() ? optional_layout(arg.bias().get_output_layout()) : optional_layout()); + set_params(param_info, r_params); r_params.layerID = arg.id() + "_reorder_"; r_params.input = convert_weights_tensor(weights_layout, cldnn_prim->grouped_weights_shape); diff --git a/src/plugins/intel_gpu/src/graph/impls/onednn/fully_connected_onednn.cpp b/src/plugins/intel_gpu/src/graph/impls/onednn/fully_connected_onednn.cpp index 600cacf615b2ae..fd97be699cc4db 100644 --- a/src/plugins/intel_gpu/src/graph/impls/onednn/fully_connected_onednn.cpp +++ b/src/plugins/intel_gpu/src/graph/impls/onednn/fully_connected_onednn.cpp @@ -60,12 +60,13 @@ struct fully_connected_onednn : typed_primitive_onednn_impl : public typed_program_node_base get_kernel_impl_params(const std::vector& in_layouts, const layout& out_layout) const override { - const auto& bias_layout = - bias_term() ? bias().get_output_layout() : layout(data_types::f32, format::any, tensor()); - return std::make_shared(get_program(), get_primitive(), get_unique_id(), in_layouts, out_layout, get_fused_primitives(), get_fused_activations_funcs(), get_fused_activations_params(), - weights().get_output_layout(), bias_term(), bias_layout); + optional_layout(weights().get_output_layout())); } private: diff --git a/src/plugins/intel_gpu/src/graph/include/convolution_inst.h b/src/plugins/intel_gpu/src/graph/include/convolution_inst.h index 37fa185735b632..8d7f50cf1d8ac1 100644 --- a/src/plugins/intel_gpu/src/graph/include/convolution_inst.h +++ b/src/plugins/intel_gpu/src/graph/include/convolution_inst.h @@ -115,28 +115,20 @@ struct typed_program_node : public typed_program_node_basebias.size() > 0; } - bool weights_zero_points_term() const { return get_primitive()->weights_zero_points.size() > 0; } bool compensation_term() const { return get_primitive()->compensation.size() > 0; } bool activations_zero_points_term() const { return get_primitive()->activations_zero_points.size() > 0; } std::shared_ptr get_kernel_impl_params(const std::vector& in_layouts, const layout& out_layout) const override { - const auto& bias_layout = bias_term() ? bias().get_output_layout() : layout(data_types::f32, format::any, tensor()); - const auto& weights_zero_points_layout = weights_zero_points_term() ? weights_zero_points().get_output_layout() - : layout(data_types::f32, format::any, tensor()); - const auto& activations_zero_points_layout = activations_zero_points_term() ? activations_zero_points().get_output_layout() - : layout(data_types::f32, format::any, tensor()); - const auto& compensation_layout = compensation_term() ? compensation().get_output_layout() - : layout(data_types::f32, format::any, tensor()); - return std::make_shared(get_program(), get_primitive(), get_unique_id(), in_layouts, out_layout, get_fused_primitives(), get_fused_activations_funcs(), get_fused_activations_params(), - weights().get_output_layout(), bias_term(), bias_layout, - weights_zero_points_term(), weights_zero_points_layout, - activations_zero_points_term(), activations_zero_points_layout, - compensation_term(), compensation_layout); + optional_layout(weights().get_output_layout()), + bias_term() ? optional_layout(bias().get_output_layout()) : optional_layout(), + weights_zero_points_term() ? optional_layout(weights_zero_points().get_output_layout()) : optional_layout(), + activations_zero_points_term() ? optional_layout(activations_zero_points().get_output_layout()) : optional_layout(), + compensation_term() ? optional_layout(compensation().get_output_layout()) : optional_layout()); } private: diff --git a/src/plugins/intel_gpu/src/graph/include/deconvolution_inst.h b/src/plugins/intel_gpu/src/graph/include/deconvolution_inst.h index 3a17501322013f..1bdee4208aa3f5 100644 --- a/src/plugins/intel_gpu/src/graph/include/deconvolution_inst.h +++ b/src/plugins/intel_gpu/src/graph/include/deconvolution_inst.h @@ -75,11 +75,11 @@ struct typed_program_node : public typed_program_node_base get_kernel_impl_params(const std::vector& in_layouts, const layout& out_layout) const override { - const auto& bias_layout = bias_term() ? bias().get_output_layout() : layout(data_types::f32, format::any, tensor()); return std::make_shared(get_program(), get_primitive(), get_unique_id(), in_layouts, out_layout, get_fused_primitives(), get_fused_activations_funcs(), get_fused_activations_params(), - weights().get_output_layout(), bias_term(), bias_layout); + optional_layout(weights().get_output_layout()), + bias_term() ? optional_layout(bias().get_output_layout()) : optional_layout()); } private: diff --git a/src/plugins/intel_gpu/src/graph/include/deformable_convolution_inst.h b/src/plugins/intel_gpu/src/graph/include/deformable_convolution_inst.h index fc74ed70e1c80e..6d857c9f2ba525 100644 --- a/src/plugins/intel_gpu/src/graph/include/deformable_convolution_inst.h +++ b/src/plugins/intel_gpu/src/graph/include/deformable_convolution_inst.h @@ -58,12 +58,11 @@ struct typed_program_node : public typed_program_node_base get_kernel_impl_params(const std::vector& in_layouts, const layout& out_layout) const override { - const auto& bias_layout = bias_term() ? bias().get_output_layout() : layout(data_types::f32, format::any, tensor()); - return std::make_shared(get_program(), get_primitive(), get_unique_id(), in_layouts, out_layout, get_fused_primitives(), get_fused_activations_funcs(), get_fused_activations_params(), - weights().get_output_layout(), bias_term(), bias_layout); + optional_layout(weights().get_output_layout()), + bias_term() ? optional_layout(bias().get_output_layout()) : optional_layout()); } private: diff --git a/src/plugins/intel_gpu/src/graph/include/fully_connected_inst.h b/src/plugins/intel_gpu/src/graph/include/fully_connected_inst.h index 559f2670239b50..4bdff132b7a47f 100644 --- a/src/plugins/intel_gpu/src/graph/include/fully_connected_inst.h +++ b/src/plugins/intel_gpu/src/graph/include/fully_connected_inst.h @@ -25,11 +25,11 @@ struct typed_program_node : public typed_program_node_basebias.empty(); } std::shared_ptr get_kernel_impl_params(const std::vector& in_layouts, const layout& out_layout) const override { - const auto& bias_layout = bias_term() ? bias().get_output_layout() : layout(data_types::f32, format::any, tensor()); return std::make_shared(get_program(), get_primitive(), get_unique_id(), in_layouts, out_layout, get_fused_primitives(), get_fused_activations_funcs(), get_fused_activations_params(), - weights().get_output_layout(), bias_term(), bias_layout); + optional_layout(weights().get_output_layout()), + bias_term() ? optional_layout(bias().get_output_layout()) : optional_layout()); } }; diff --git a/src/plugins/intel_gpu/src/graph/include/kernel_selector_helper.h b/src/plugins/intel_gpu/src/graph/include/kernel_selector_helper.h index 050218b6b02f80..958e051db5aac0 100644 --- a/src/plugins/intel_gpu/src/graph/include/kernel_selector_helper.h +++ b/src/plugins/intel_gpu/src/graph/include/kernel_selector_helper.h @@ -119,38 +119,32 @@ struct kernel_impl_params { std::vector fused_act_funcs; std::vector activation_params; - layout weights_layout = layout(data_types::f32, format::any, tensor()); + //optional_layout weights_layout = layout(data_types::f32, format::any, tensor()); + optional_layout weights_layout; - bool bias_term = false; - layout bias_layout = layout(data_types::f32, format::any, tensor()); - - bool weights_zero_points_term = false; - layout weights_zero_points_layout = layout(data_types::f32, format::any, tensor()); - - bool activations_zero_points_term = false; - layout activations_zero_points_layout = layout(data_types::f32, format::any, tensor()); - - bool compensation_term = false; - layout compensation_layout = layout(data_types::f32, format::any, tensor()); + optional_layout bias_layout; + optional_layout weights_zero_points_layout; + optional_layout activations_zero_points_layout; + optional_layout compensation_layout; kernel_impl_params(program& _prog, std::shared_ptr _desc, size_t _uid, const std::vector& _int_layouts, layout _out_layout, const std::vector& _fused_descs, const std::vector& _fused_act_funcs, const std::vector& _act_params, - layout _weights_layout = layout(data_types::f32, format::any, tensor()), - bool _bias_term = false, layout _bias_layout = layout(data_types::f32, format::any, tensor()), - bool _weights_zero_points_term = false, layout _weights_zero_points_layout = layout(data_types::f32, format::any, tensor()), - bool _activations_zero_points_term = false, layout _activations_zero_points_layout = layout(data_types::f32, format::any, tensor()), - bool _compensation_term = false, layout _compensation_layout = layout(data_types::f32, format::any, tensor())) + optional_layout _weights_layout = optional_layout(), + optional_layout _bias_layout = optional_layout(), + optional_layout _weights_zero_points_layout = optional_layout(), + optional_layout _activations_zero_points_layout = optional_layout(), + optional_layout _compensation_layout = optional_layout()) : has_runtime_layouts(true), prog(_prog), desc(_desc), unique_id(_uid), input_layouts(_int_layouts), output_layout(_out_layout), fused_desc(_fused_descs), fused_act_funcs(_fused_act_funcs), activation_params(_act_params), - weights_layout(_weights_layout), bias_term(_bias_term), bias_layout(_bias_layout), - weights_zero_points_term(_weights_zero_points_term), weights_zero_points_layout(_weights_zero_points_layout), - activations_zero_points_term(_activations_zero_points_term), activations_zero_points_layout(_activations_zero_points_layout), - compensation_term(_compensation_term), compensation_layout(_compensation_layout) {} + weights_layout(_weights_layout), bias_layout(_bias_layout), + weights_zero_points_layout(_weights_zero_points_layout), + activations_zero_points_layout(_activations_zero_points_layout), + compensation_layout(_compensation_layout) {} template std::shared_ptr typed_desc() const { return std::static_pointer_cast(desc); } @@ -265,10 +259,10 @@ template inline params_t get_weights_bias_default_params(const kernel_impl_params& param_info, uint32_t split = 1, uint32_t groups = 1, bool has_group_dimension = false) { params_t params = get_default_params(param_info, split); - params.weights = convert_weights_tensor(param_info.weights_layout, has_group_dimension); + params.weights = convert_weights_tensor(*param_info.weights_layout, has_group_dimension); - if (param_info.bias_term) { - auto bias_layout = param_info.bias_layout; + if (param_info.bias_layout) { + auto bias_layout = *param_info.bias_layout; if (groups != 1) { bias_layout.size.feature[0] /= static_cast(groups); } @@ -283,21 +277,21 @@ params_t get_weight_bias_zero_point_default_params(const kernel_impl_params& par bool has_group_dimension = false) { params_t params = get_weights_bias_default_params(param_info, split, groups, has_group_dimension); - if (param_info.weights_zero_points_term) { + if (param_info.weights_zero_points_layout) { params.weights_zero_points.push_back( - convert_data_tensor(param_info.weights_zero_points_layout) + convert_data_tensor(*param_info.weights_zero_points_layout) .FlattenFeatureAndSpatials()); } - if (param_info.activations_zero_points_term) { + if (param_info.activations_zero_points_layout) { params.activations_zero_points.push_back( - convert_data_tensor(param_info.activations_zero_points_layout) + convert_data_tensor(*param_info.activations_zero_points_layout) .FlattenFeatureAndSpatials()); } - if (param_info.compensation_term) { + if (param_info.compensation_layout) { params.compensation.push_back( - convert_data_tensor(param_info.compensation_layout).FlattenFeatureAndSpatials()); + convert_data_tensor(*param_info.compensation_layout).FlattenFeatureAndSpatials()); } return params;