OpenVINO™ is an open source toolkit for optimizing and deploying AI inference
C++ aicomputer-visiondeep-learningdeploy-aidiffusion-modelsgenerative-aigood-first-issueinferencellm-inferencenatural-language-processingnlpopenvinooptimize-aiperformance-boostrecommendation-systemspeech-recognitionstable-diffusiontransformersyolo
[GPU] Reuse equivalent custom kernels across layer names (#39450)
Rhonin Wangdace268bdd36
diff --git a/src/plugins/intel_gpu/include/intel_gpu/plugin/custom_layer_source.hpp b/src/plugins/intel_gpu/include/intel_gpu/plugin/custom_layer_source.hppnew file mode 100644index 00000000000..ff00bf6608a--- /dev/null+++ b/src/plugins/intel_gpu/include/intel_gpu/plugin/custom_layer_source.hpp@@ -0,0 +1,125 @@+// Copyright (C) 2018-2026 Intel Corporation+// SPDX-License-Identifier: Apache-2.0+//++#pragma once++#include "openvino/core/attribute_visitor.hpp"+#include "openvino/core/node.hpp"+#include "intel_gpu/plugin/custom_layer.hpp"++namespace ov::intel_gpu {+namespace custom_layer_source_detail {++template<typename T>+static inline std::string vecToString(std::vector<T> vec) {+ if (vec.empty()) {+ return "";+ }++ std::string res = std::to_string(vec[0]);+ for (size_t i = 1; i < vec.size(); i++) {+ res += "," + std::to_string(vec[i]);+ }+ return res;+}++template<>+inline std::string vecToString<std::string>(std::vector<std::string> vec) {+ if (vec.empty()) {+ return "";+ }++ std::string res = vec[0];+ for (size_t i = 1; i < vec.size(); i++) {+ res += "," + vec[i];+ }+ return res;+}++class CustomLayerAttributeVisitor : public ov::AttributeVisitor {+public:+ CustomLayerAttributeVisitor() : m_values({}) { }++ void on_adapter(const std::string& name, ov::ValueAccessor<void>& adapter) override {+ OPENVINO_THROW("Attribute ", name, " can't be processed\n");+ }+ // The remaining adapter methods fall back on the void adapter if not implemented+ void on_adapter(const std::string& name, ov::ValueAccessor<std::string>& adapter) override {+ m_values[name] = adapter.get();+ }+ void on_adapter(const std::string& name, ov::ValueAccessor<bool>& adapter) override {+ m_values[name] = std::to_string(static_cast<int>(adapter.get()));+ }+ void on_adapter(const std::string& name, ov::ValueAccessor<int64_t>& adapter) override {+ m_values[name] = std::to_string(adapter.get());+ }+ void on_adapter(const std::string& name, ov::ValueAccessor<double>& adapter) override {+ m_values[name] = std::to_string(adapter.get());+ }+ void on_adapter(const std::string& name, ov::ValueAccessor<std::vector<std::string>>& adapter) override {+ m_values[name] = vecToString(adapter.get());+ }+ void on_adapter(const std::string& name, ov::ValueAccessor<std::vector<float>>& adapter) override {+ m_values[name] = vecToString(adapter.get());+ }+ void on_adapter(const std::string& name, ov::ValueAccessor<std::vector<double>>& adapter) override {+ m_values[name] = vecToString(adapter.get());+ }+ void on_adapter(const std::string& name, ov::ValueAccessor<std::vector<int8_t>>& adapter) override {+ m_values[name] = vecToString(adapter.get());+ }+ void on_adapter(const std::string& name, ov::ValueAccessor<std::vector<int16_t>>& adapter) override {+ m_values[name] = vecToString(adapter.get());+ }+ void on_adapter(const std::string& name, ov::ValueAccessor<std::vector<int32_t>>& adapter) override {+ m_values[name] = vecToString(adapter.get());+ }+ void on_adapter(const std::string& name, ov::ValueAccessor<std::vector<int64_t>>& adapter) override {+ m_values[name] = vecToString(adapter.get());+ }+ void on_adapter(const std::string& name, ov::ValueAccessor<std::vector<uint8_t>>& adapter) override {+ m_values[name] = vecToString(adapter.get());+ }+ void on_adapter(const std::string& name, ov::ValueAccessor<std::vector<uint16_t>>& adapter) override {+ m_values[name] = vecToString(adapter.get());+ }+ void on_adapter(const std::string& name, ov::ValueAccessor<std::vector<uint32_t>>& adapter) override {+ m_values[name] = vecToString(adapter.get());+ }+ void on_adapter(const std::string& name, ov::ValueAccessor<std::vector<uint64_t>>& adapter) override {+ m_values[name] = vecToString(adapter.get());+ }++ std::map<std::string, std::string> get_parameters() const {+ return m_values;+ }++protected:+ std::map<std::string, std::string> m_values;+};++} // namespace custom_layer_source_detail++// This is the source-generation path used by CreateCustomOp. Instance names stay+// in primitive IDs, while source identity depends on the layer type and defines.+inline std::vector<std::string> make_custom_layer_source(const std::shared_ptr<ov::Node>& op,+ const CustomLayer& custom_layer) {+ custom_layer_source_detail::CustomLayerAttributeVisitor visitor;+ op->visit_attributes(visitor);+ const auto params = visitor.get_parameters();+ std::string defines;+ for (const auto& def : custom_layer.Defines()) {+ std::string value = "#define " + def.name + " " + def.prefix;+ auto parameter = params.find(def.param);+ value += parameter != params.end() ? parameter->second : def.default_value;+ value += def.postfix + "\n";+ defines += value;+ }+ return {"\n// Custom Layer " + custom_layer.Name() + "\n",+ "// Custom Layer User Defines\n",+ defines,+ custom_layer.KernelSource()};+}++} // namespace ov::intel_gpudiff --git a/src/plugins/intel_gpu/src/plugin/ops/custom.cpp b/src/plugins/intel_gpu/src/plugin/ops/custom.cppindex 8ebf54132cb..2c86bffdcd9 100755--- a/src/plugins/intel_gpu/src/plugin/ops/custom.cpp+++ b/src/plugins/intel_gpu/src/plugin/ops/custom.cpp@@ -6,6 +6,7 @@#include "openvino/core/node.hpp"#include "intel_gpu/plugin/program_builder.hpp"+#include "intel_gpu/plugin/custom_layer_source.hpp"#include "intel_gpu/plugin/common_utils.hpp"#include "intel_gpu/plugin/simple_math.hpp"#include "intel_gpu/primitives/custom_gpu_primitive.hpp"@@ -13,94 +14,6 @@namespace ov::intel_gpu {-template<typename T>-static inline std::string vecToString(std::vector<T> vec) {- if (vec.empty()) {- return "";- }-- std::string res = std::to_string(vec[0]);- for (size_t i = 1; i < vec.size(); i++) {- res += "," + std::to_string(vec[i]);- }- return res;-}--template<>-inline std::string vecToString<std::string>(std::vector<std::string> vec) {- if (vec.empty()) {- return "";- }-- std::string res = vec[0];- for (size_t i = 1; i < vec.size(); i++) {- res += "," + vec[i];- }- return res;-}--class CustomLayerAttributeVisitor : public ov::AttributeVisitor {-public:- CustomLayerAttributeVisitor() : m_values({}) { }-- void on_adapter(const std::string& name, ov::ValueAccessor<void>& adapter) override {- OPENVINO_THROW("Attribute ", name, " can't be processed\n");- }- // The remaining adapter methods fall back on the void adapter if not implemented- void on_adapter(const std::string& name, ov::ValueAccessor<std::string>& adapter) override {- m_values[name] = adapter.get();- }- void on_adapter(const std::string& name, ov::ValueAccessor<bool>& adapter) override {- m_values[name] = std::to_string(static_cast<int>(adapter.get()));- }- void on_adapter(const std::string& name, ov::ValueAccessor<int64_t>& adapter) override {- m_values[name] = std::to_string(adapter.get());- }- void on_adapter(const std::string& name, ov::ValueAccessor<double>& adapter) override {- m_values[name] = std::to_string(adapter.get());- }- void on_adapter(const std::string& name, ov::ValueAccessor<std::vector<std::string>>& adapter) override {- m_values[name] = vecToString(adapter.get());- }- void on_adapter(const std::string& name, ov::ValueAccessor<std::vector<float>>& adapter) override {- m_values[name] = vecToString(adapter.get());- }- void on_adapter(const std::string& name, ov::ValueAccessor<std::vector<double>>& adapter) override {- m_values[name] = vecToString(adapter.get());- }- void on_adapter(const std::string& name, ov::ValueAccessor<std::vector<int8_t>>& adapter) override {- m_values[name] = vecToString(adapter.get());- }- void on_adapter(const std::string& name, ov::ValueAccessor<std::vector<int16_t>>& adapter) override {- m_values[name] = vecToString(adapter.get());- }- void on_adapter(const std::string& name, ov::ValueAccessor<std::vector<int32_t>>& adapter) override {- m_values[name] = vecToString(adapter.get());- }- void on_adapter(const std::string& name, ov::ValueAccessor<std::vector<int64_t>>& adapter) override {- m_values[name] = vecToString(adapter.get());- }- void on_adapter(const std::string& name, ov::ValueAccessor<std::vector<uint8_t>>& adapter) override {- m_values[name] = vecToString(adapter.get());- }- void on_adapter(const std::string& name, ov::ValueAccessor<std::vector<uint16_t>>& adapter) override {- m_values[name] = vecToString(adapter.get());- }- void on_adapter(const std::string& name, ov::ValueAccessor<std::vector<uint32_t>>& adapter) override {- m_values[name] = vecToString(adapter.get());- }- void on_adapter(const std::string& name, ov::ValueAccessor<std::vector<uint64_t>>& adapter) override {- m_values[name] = vecToString(adapter.get());- }-- std::map<std::string, std::string> get_parameters() const {- return m_values;- }--protected:- std::map<std::string, std::string> m_values;-};-void CreateCustomOp(ProgramBuilder& p, const std::shared_ptr<ov::Node>& op, CustomLayerPtr customLayer) {if (!p.use_new_shape_infer()) {OPENVINO_ASSERT(op->get_output_size() == 1u, "Custom OP limitation: static model only support one output.");@@ -108,22 +21,7 @@ void CreateCustomOp(ProgramBuilder& p, const std::shared_ptr<ov::Node>& op, Custauto inputs = p.GetInputInfo(op);std::string layerName = layer_type_name_ID(op);- CustomLayerAttributeVisitor visitor;- op->visit_attributes(visitor);- auto params = visitor.get_parameters();-- // Handle defines- std::string layerDefines;- for (const auto& def : customLayer->Defines()) {- std::string singleDefine("#define " + def.name + " " + def.prefix);- if (params.find(def.param) != params.end()) {- singleDefine += params.at(def.param);- } else {- singleDefine += def.default_value;- }- singleDefine += def.postfix + "\n";- layerDefines.append(singleDefine);- }+ const auto kernel_sources = make_custom_layer_source(op, *customLayer);// reservestd::vector<cldnn::input_info> reordered_inputs;@@ -178,9 +76,6 @@ void CreateCustomOp(ProgramBuilder& p, const std::shared_ptr<ov::Node>& op, CustOPENVINO_THROW("Invalid custom layer param type: ", param.type, " in operation: ", op->get_friendly_name());}}- const std::string layerTitle("\n// Layer " + op->get_friendly_name() + " using Custom Layer " + customLayer->Name() + "\n");- const std::string defineTitle("// Custom Layer User Defines\n");-int iidx = customLayer->InputDimSourceIndex();OPENVINO_ASSERT(outputFormats.size() == op->get_output_size(), "The number of outputFormats should be same as op->get_output_size().");@@ -242,7 +137,7 @@ void CreateCustomOp(ProgramBuilder& p, const std::shared_ptr<ov::Node>& op, Custauto customPrim = cldnn::custom_gpu_primitive(genericLayerName,reordered_inputs,- {layerTitle, defineTitle, layerDefines, customLayer->KernelSource()},+ kernel_sources,customLayer->KernelEntry(),kernelParameters,customLayer->CompilerOptions(),diff --git a/src/plugins/intel_gpu/tests/unit/CMakeLists.txt b/src/plugins/intel_gpu/tests/unit/CMakeLists.txtindex e7a82878410..44103808a7c 100644--- a/src/plugins/intel_gpu/tests/unit/CMakeLists.txt+++ b/src/plugins/intel_gpu/tests/unit/CMakeLists.txt@@ -66,6 +66,7 @@ ov_add_test_target(${TEST_COMMON_INCLUDE_DIR}LINK_LIBRARIESopenvino_intel_gpu_graph+ openvino::pugixmlgtestgtest_maingflagsdiff --git a/src/plugins/intel_gpu/tests/unit/test_cases/custom_gpu_primitive_test.cpp b/src/plugins/intel_gpu/tests/unit/test_cases/custom_gpu_primitive_test.cppindex db31fdfcf79..3a6eea26c88 100644--- a/src/plugins/intel_gpu/tests/unit/test_cases/custom_gpu_primitive_test.cpp+++ b/src/plugins/intel_gpu/tests/unit/test_cases/custom_gpu_primitive_test.cpp@@ -4,6 +4,8 @@#include "graph/include/primitive_inst.h"#include "test_utils.h"+#include "intel_gpu/plugin/custom_layer_source.hpp"+#include "openvino/op/parameter.hpp"#include <intel_gpu/primitives/input_layout.hpp>#include <intel_gpu/primitives/eltwise.hpp>@@ -933,3 +935,72 @@ TEST(custom_gpu_primitive_f32, documented_pitch_indexing_reads_non_contiguous_infor (size_t i = 0; i < expected.size(); i++)ASSERT_EQ(output_ptr[i], expected[i]) << "element " << i;}++namespace custom_gpu_primitive_source_test {++class CustomSourceOp : public ov::op::Op {+public:+ OPENVINO_OP("CustomSourceOp", "gpu_test");++ CustomSourceOp(const ov::Output<ov::Node>& input, float alpha) : Op({input}), m_alpha(alpha) {+ constructor_validate_and_infer_types();+ }++ void validate_and_infer_types() override {+ set_output_type(0, get_input_element_type(0), get_input_partial_shape(0));+ }++ bool visit_attributes(ov::AttributeVisitor& visitor) override {+ visitor.on_attribute("alpha", m_alpha);+ return true;+ }++ std::shared_ptr<ov::Node> clone_with_new_inputs(const ov::OutputVector& inputs) const override {+ return std::make_shared<CustomSourceOp>(inputs.at(0), m_alpha);+ }++private:+ float m_alpha;+};++class SourceCacheCustomLayer : public ov::intel_gpu::CustomLayer {+public:+ SourceCacheCustomLayer() {+ m_layerName = "CustomSourceOp";+ m_kernelSource = "__kernel void custom_source(__global float* x) { x[0] *= ALPHA; }";+ KernelDefine alpha;+ alpha.name = "ALPHA";+ alpha.param = "alpha";+ alpha.default_value = "1";+ m_defines.push_back(alpha);+ }+};++TEST(custom_gpu_primitive_source, friendly_names_do_not_change_generated_source) {+ auto input = std::make_shared<ov::op::v0::Parameter>(ov::element::f32, ov::Shape{1, 1, 2, 2});+ auto first = std::make_shared<CustomSourceOp>(input, 1.0f);+ auto second = std::make_shared<CustomSourceOp>(input, 1.0f);+ first->set_friendly_name("first_instance");+ second->set_friendly_name("second_instance");+ SourceCacheCustomLayer layer;++ const auto first_source = ov::intel_gpu::make_custom_layer_source(first, layer);+ const auto second_source = ov::intel_gpu::make_custom_layer_source(second, layer);+ ASSERT_EQ(first_source, second_source);+}++TEST(custom_gpu_primitive_source, different_defines_keep_generated_source_distinct) {+ auto input = std::make_shared<ov::op::v0::Parameter>(ov::element::f32, ov::Shape{1, 1, 2, 2});+ auto first = std::make_shared<CustomSourceOp>(input, 1.0f);+ auto second = std::make_shared<CustomSourceOp>(input, 2.0f);+ // Hold the name equal so this control checks the attribute-derived define.+ first->set_friendly_name("same_instance_name");+ second->set_friendly_name("same_instance_name");+ SourceCacheCustomLayer layer;++ const auto first_source = ov::intel_gpu::make_custom_layer_source(first, layer);+ const auto second_source = ov::intel_gpu::make_custom_layer_source(second, layer);+ ASSERT_NE(first_source, second_source);+}++} // namespace custom_gpu_primitive_source_test