Skip to content

OpenVINO™ is an open source toolkit for optimizing and deploying AI inference

[GPU] Enable Custom OP to support multiple outputs. (#32583)

Xiping Yan72f6fe1bec70
diff --git a/src/plugins/intel_gpu/include/intel_gpu/primitives/custom_gpu_primitive.hpp b/src/plugins/intel_gpu/include/intel_gpu/primitives/custom_gpu_primitive.hpp
index 323f2fe889c..b2ac166dd0a 100644
--- a/src/plugins/intel_gpu/include/intel_gpu/primitives/custom_gpu_primitive.hpp
+++ b/src/plugins/intel_gpu/include/intel_gpu/primitives/custom_gpu_primitive.hpp
@@ -109,19 +109,19 @@ struct custom_gpu_primitive : public primitive_base<custom_gpu_primitive> {
const std::string& kernel_entry_point,
const std::vector<arg_desc>& kernel_arguments,
const std::string& build_options,
- const layout& output_layout,
+ const std::vector<layout>& output_layouts,
const std::vector<size_t>& gws = {},
const std::vector<size_t>& lws = {},
const std::shared_ptr<ov::Node>& op = nullptr,
const int calcWgDimInputIdx = -1,
const std::vector<std::string> globalSizeRules = {},
const std::vector<std::string> localSizeRules = {})
- : primitive_base(id, inputs, 1, {optional_data_type()}, {output_layout.data_padding}),
+ : primitive_base(id, inputs, output_layouts.size(), {optional_data_type()}, {output_layouts[0].data_padding}),
kernel_entry_point(kernel_entry_point),
kernel_arguments(kernel_arguments),
build_options(build_options),
- output_layout(output_layout),
- gws(gws.size() ? gws : std::vector<size_t>{output_layout.count()}),
+ output_layouts(output_layouts),
+ gws(gws.size() ? gws : std::vector<size_t>{output_layouts[0].count()}),
lws(lws),
kernels_code(kernels_code),
op(op),
@@ -136,7 +136,7 @@ struct custom_gpu_primitive : public primitive_base<custom_gpu_primitive> {
/// @brief The kernel's build options
const std::string build_options;
/// @brief The output layout declared by the primitive
- const layout output_layout;
+ const std::vector<layout> output_layouts;
/// @brief The global working sizes
const std::vector<size_t> gws;
/// @brief The local working sizes
@@ -197,7 +197,7 @@ struct custom_gpu_primitive : public primitive_base<custom_gpu_primitive> {
ob << kernel_entry_point;
ob << kernel_arguments;
ob << build_options;
- ob << output_layout;
+ ob << output_layouts;
ob << gws;
ob << lws;
ob << kernels_code;
@@ -208,7 +208,7 @@ struct custom_gpu_primitive : public primitive_base<custom_gpu_primitive> {
ib >> *const_cast<std::string*>(&kernel_entry_point);
ib >> *const_cast<std::vector<arg_desc>*>(&kernel_arguments);
ib >> *const_cast<std::string*>(&build_options);
- ib >> *const_cast<layout*>(&output_layout);
+ ib >> *const_cast<std::vector<layout>*>(&output_layouts);
ib >> *const_cast<std::vector<size_t>*>(&gws);
ib >> *const_cast<std::vector<size_t>*>(&lws);
ib >> *const_cast<primitive_id_arr*>(&kernels_code);
diff --git a/src/plugins/intel_gpu/src/graph/impls/ocl/custom_primitive.cpp b/src/plugins/intel_gpu/src/graph/impls/ocl/custom_primitive.cpp
index 8832b91d913..03c23ba1088 100644
--- a/src/plugins/intel_gpu/src/graph/impls/ocl/custom_primitive.cpp
+++ b/src/plugins/intel_gpu/src/graph/impls/ocl/custom_primitive.cpp
@@ -88,7 +88,11 @@ struct custom_gpu_primitive_impl : typed_primitive_impl<custom_gpu_primitive> {
for (auto& dep : instance.dependencies()) {
args.inputs.push_back(dep.first->output_memory_ptr());
}
- args.outputs = { instance.output_memory_ptr() };
+
+ for (size_t i = 0; i < instance.outputs_memory_count(); i++) {
+ args.outputs.push_back(instance.output_memory_ptr(i));
+ }
+
stream.set_arguments(*_kernels.front(), cl_kernel.get()->params, args);
}
@@ -99,7 +103,11 @@ struct custom_gpu_primitive_impl : typed_primitive_impl<custom_gpu_primitive> {
for (auto& dep : instance.dependencies()) {
args.inputs.push_back(dep.first->output_memory_ptr());
}
- args.outputs = { instance.output_memory_ptr() };
+
+ for (size_t i = 0; i < instance.outputs_memory_count(); i++) {
+ args.outputs.push_back(instance.output_memory_ptr(i));
+ }
+
return stream.enqueue_kernel(*_kernels.front(), cl_kernel.get()->params, args, events, instance.is_output());
}
@@ -238,7 +246,9 @@ static std::string get_jit_constant(const custom_gpu_primitive_node& outer,
add_layout_to_jit(mem_consts, "INPUT" + std::to_string(i), impl_param.get_input_layout(i));
}
- add_layout_to_jit(mem_consts, "OUTPUT0", impl_param.get_output_layout());
+ for (size_t i = 0; i < impl_param.output_layouts.size(); i++) {
+ add_layout_to_jit(mem_consts, "OUTPUT" + std::to_string(i), impl_param.get_output_layout(i));
+ }
std::ostringstream oss;
oss << "// Custom Layer Built-ins\n\n";
diff --git a/src/plugins/intel_gpu/src/graph/include/custom_gpu_primitive_inst.h b/src/plugins/intel_gpu/src/graph/include/custom_gpu_primitive_inst.h
index 1b0f84d7b28..2a69f135fd8 100644
--- a/src/plugins/intel_gpu/src/graph/include/custom_gpu_primitive_inst.h
+++ b/src/plugins/intel_gpu/src/graph/include/custom_gpu_primitive_inst.h
@@ -34,31 +34,33 @@ public:
assert(static_cast<bool>(impl_param.desc->output_data_types[0]) == false &&
"Output data type forcing is not supported for "
"custom_gpu_primitive_node!");
- layout output_layout = impl_param.typed_desc<custom_gpu_primitive>()->output_layout;
+ std::vector<layout> output_layouts = impl_param.typed_desc<custom_gpu_primitive>()->output_layouts;
- typed_primitive_inst<custom_gpu_primitive>::update_output_shape(impl_param, output_layout);
+ typed_primitive_inst<custom_gpu_primitive>::update_output_shape(impl_param, output_layouts);
// if the output layout format was set to any, it means the layer output format will be the same as the first input
- if (output_layout.format == format::any) {
- output_layout.format = impl_param.get_input_layout().format;
+ for (auto& output_layout : output_layouts) {
+ if (output_layout.format == format::any) {
+ output_layout.format = impl_param.get_input_layout().format;
+ }
}
- return { output_layout };
+ return output_layouts;
}
static layout calc_output_layout(custom_gpu_primitive_node const& node, kernel_impl_params const& impl_param) {
assert(static_cast<bool>(impl_param.desc->output_data_types[0]) == false &&
"Output data type forcing is not supported for "
"custom_gpu_primitive_node!");
- layout output_layout = impl_param.typed_desc<custom_gpu_primitive>()->output_layout;
+ auto output_layouts = impl_param.typed_desc<custom_gpu_primitive>()->output_layouts;
- typed_primitive_inst<custom_gpu_primitive>::update_output_shape(impl_param, output_layout);
+ typed_primitive_inst<custom_gpu_primitive>::update_output_shape(impl_param, output_layouts);
// if the output layout format was set to any, it means the layer output format will be the same as the first
// input
- if (output_layout.format == format::any) {
- output_layout.format = impl_param.get_input_layout().format;
+ if (output_layouts[0].format == format::any) {
+ output_layouts[0].format = impl_param.get_input_layout().format;
}
- return output_layout;
+ return output_layouts[0];
}
static std::string to_string(custom_gpu_primitive_node const& node);
@@ -67,7 +69,7 @@ public:
typed_primitive_inst(network& network, custom_gpu_primitive_node const& node);
private:
- static void update_output_shape(const kernel_impl_params& impl_param, layout& output_layout) {
+ static void update_output_shape(const kernel_impl_params& impl_param, std::vector<layout>& output_layouts) {
bool is_dynamic_input = false;
const auto inp_sz = impl_param.get_input_layout_size();
for (size_t i = 0; i < inp_sz; i++) {
@@ -77,9 +79,17 @@ private:
}
}
+ bool is_dynamic_output = false;
+ for (size_t i = 0; i < output_layouts.size(); i++) {
+ if (output_layouts[i].is_dynamic()) {
+ is_dynamic_output = true;
+ break;
+ }
+ }
+
// Execute the op's shape inference only for dynamic node when input shapes have already been calculated; otherwise, keep the original output layout
// unchanged (it will be either static for static model or have dynamic shape in case of dynamic flow)
- if (!is_dynamic_input && output_layout.is_dynamic()) {
+ if (!is_dynamic_input && is_dynamic_output) {
ov::OutputVector new_inputs;
for (size_t i = 0; i < inp_sz; i++) {
auto input = std::make_shared<ov::op::v0::Parameter>(impl_param.get_input_layout(i).data_type, impl_param.get_input_layout(i).get_shape());
@@ -89,8 +99,11 @@ private:
auto op = impl_param.typed_desc<custom_gpu_primitive>()->op;
auto new_op = op->clone_with_new_inputs(new_inputs);
new_op->validate_and_infer_types();
- auto new_outp_shape = new_op->get_output_shape(0);
- output_layout.set_partial_shape(new_outp_shape);
+ OPENVINO_ASSERT(output_layouts.size() == new_op->get_output_size(), "Output size should keep align.");
+ for (size_t i = 0; i < output_layouts.size(); i++) {
+ auto new_outp_shape = new_op->get_output_shape(i);
+ output_layouts[i].set_partial_shape(new_outp_shape);
+ }
}
}
};
diff --git a/src/plugins/intel_gpu/src/plugin/ops/custom.cpp b/src/plugins/intel_gpu/src/plugin/ops/custom.cpp
index a810953bc5c..6f428ae5cd6 100644
--- a/src/plugins/intel_gpu/src/plugin/ops/custom.cpp
+++ b/src/plugins/intel_gpu/src/plugin/ops/custom.cpp
@@ -100,6 +100,9 @@ protected:
};
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.");
+ }
auto inputs = p.GetInputInfo(op);
std::string layerName = layer_type_name_ID(op);
@@ -126,7 +129,7 @@ void CreateCustomOp(ProgramBuilder& p, const std::shared_ptr<ov::Node>& op, Cust
// Handle kernel parameters
std::vector<cldnn::custom_gpu_primitive::arg_desc> kernelParameters;
- cldnn::format outputFormat(cldnn::format::any);
+ std::vector<cldnn::format> outputFormats;
for (const auto& param : customLayer->KernelParams()) {
switch (param.type) {
case CustomLayer::ParamType::Input: {
@@ -159,7 +162,7 @@ void CreateCustomOp(ProgramBuilder& p, const std::shared_ptr<ov::Node>& op, Cust
kernelParameters[param.paramIndex].type = cldnn::custom_gpu_primitive::arg_output;
kernelParameters[param.paramIndex].index =
static_cast<cldnn::custom_gpu_primitive::arg_index>((param.portIndex >= static_cast<int>(inputs.size())) ? -1 : param.portIndex);
- outputFormat = param.format;
+ outputFormats.push_back(param.format);
break;
}
default:
@@ -169,21 +172,25 @@ void CreateCustomOp(ProgramBuilder& p, const std::shared_ptr<ov::Node>& op, Cust
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");
- auto dims = op->get_output_partial_shape(0);
int iidx = customLayer->InputDimSourceIndex();
+ OPENVINO_ASSERT(outputFormats.size() == op->get_output_size(), "The number of outputFormats should be same as op->get_output_size().");
- constexpr size_t kDynamic = std::numeric_limits<size_t>::max();
- size_t N = (dims.size() > 0) ? dims[0].is_dynamic() ? kDynamic : dims[0].get_length() : 1;
- size_t C = (dims.size() > 1) ? dims[1].is_dynamic() ? kDynamic : dims[1].get_length() : 1;
- size_t H = (dims.size() > 2) ? dims[2].is_dynamic() ? kDynamic : dims[2].get_length() : 1;
- size_t W = (dims.size() > 3) ? dims[3].is_dynamic() ? kDynamic : dims[3].get_length() : 1;
+ std::vector<cldnn::layout> outputLayouts(op->get_output_size());
+ for (size_t i = 0; i < op->get_output_size(); i++) {
+ auto dims = op->get_output_partial_shape(i);
- cldnn::layout outputLayout;
- if (dims.is_dynamic()) {
- outputLayout = cldnn::layout(dims, cldnn::element_type_to_data_type(op->get_output_element_type(0)), outputFormat);
- } else {
- cldnn::tensor outputTensor = cldnn::tensor(cldnn::batch(N), cldnn::feature(C), cldnn::spatial(W, H));
- outputLayout = cldnn::layout(cldnn::element_type_to_data_type(op->get_output_element_type(0)), outputFormat, outputTensor);
+ constexpr size_t kDynamic = std::numeric_limits<size_t>::max();
+ size_t N = (dims.size() > 0) ? dims[0].is_dynamic() ? kDynamic : dims[0].get_length() : 1;
+ size_t C = (dims.size() > 1) ? dims[1].is_dynamic() ? kDynamic : dims[1].get_length() : 1;
+ size_t H = (dims.size() > 2) ? dims[2].is_dynamic() ? kDynamic : dims[2].get_length() : 1;
+ size_t W = (dims.size() > 3) ? dims[3].is_dynamic() ? kDynamic : dims[3].get_length() : 1;
+
+ if (dims.is_dynamic()) {
+ outputLayouts[i] = cldnn::layout(dims, cldnn::element_type_to_data_type(op->get_output_element_type(i)), outputFormats[i]);
+ } else {
+ cldnn::tensor outputTensor = cldnn::tensor(cldnn::batch(N), cldnn::feature(C), cldnn::spatial(W, H));
+ outputLayouts[i] = cldnn::layout(cldnn::element_type_to_data_type(op->get_output_element_type(i)), outputFormats[i], outputTensor);
+ }
}
std::vector<size_t> gws, lws;
@@ -193,9 +200,17 @@ void CreateCustomOp(ProgramBuilder& p, const std::shared_ptr<ov::Node>& op, Cust
if (static_cast<size_t>(iidx) >= op->get_input_size())
OPENVINO_THROW("Invalid input tensor for index: ", iidx);
auto inputDims = op->get_input_shape(iidx);
- cldnn::custom_gpu_primitive::update_work_group_size(dims, iidx, inputDims, customLayer->GlobalSizeRules(), customLayer->LocalSizeRules(), gws, lws);
+ // Regardless of whether the model has one or more outputs, only the first output is used to update gws and lws.
+ cldnn::custom_gpu_primitive::update_work_group_size(op->get_output_partial_shape(0),
+ iidx,
+ inputDims,
+ customLayer->GlobalSizeRules(),
+ customLayer->LocalSizeRules(),
+ gws,
+ lws);
} else {
- cldnn::custom_gpu_primitive::update_work_group_size(dims,
+ // Regardless of whether the model has one or more outputs, only the first output is used to update gws and lws.
+ cldnn::custom_gpu_primitive::update_work_group_size(op->get_output_partial_shape(0),
iidx,
ov::PartialShape(),
customLayer->GlobalSizeRules(),
@@ -220,7 +235,7 @@ void CreateCustomOp(ProgramBuilder& p, const std::shared_ptr<ov::Node>& op, Cust
customLayer->KernelEntry(),
kernelParameters,
customLayer->CompilerOptions(),
- outputLayout,
+ outputLayouts,
gws,
lws,
op_bk,
@@ -229,15 +244,23 @@ void CreateCustomOp(ProgramBuilder& p, const std::shared_ptr<ov::Node>& op, Cust
customLayer->LocalSizeRules());
p.add_primitive(*op, customPrim);
- auto prevLayerName = genericLayerName;
- if (outputLayout.format != cldnn::format::any) {
- // Handle output reorder
- auto reorderPrimName = genericLayerName + ProgramBuilder::m_postCustomLayerTag;
- p.add_primitive(*op, cldnn::reorder(reorderPrimName,
- cldnn::input_info(genericLayerName),
- cldnn::format::get_default_format(op->get_output_partial_shape(0).size()),
- customPrim.output_layout.data_type));
- prevLayerName = reorderPrimName;
+ for (size_t i = 0; i < outputLayouts.size(); i++) {
+ if (outputLayouts[i].format != cldnn::format::any) {
+ auto default_format = cldnn::format::get_default_format(op->get_output_partial_shape(i).size());
+ if (outputLayouts.size() > 1) {
+ OPENVINO_ASSERT(default_format == outputLayouts[i].format,
+ "Multiple outputs with non-default formats are not supported because a reorder primitive cannot be inserted; the subsequent "
+ "nodes would fail to retrieve the correct output port index during shape inference after the graph transformation.");
+ } else {
+ // Handle output reorder
+ auto reorderPrimName = genericLayerName + ProgramBuilder::m_postCustomLayerTag + std::string("_output_") + std::to_string(i);
+ auto reorderPrim = cldnn::reorder(reorderPrimName,
+ cldnn::input_info(genericLayerName, static_cast<int>(i)),
+ default_format,
+ customPrim.output_layouts[i].data_type);
+ p.add_primitive(*op, reorderPrim);
+ }
+ }
}
}
diff --git a/src/plugins/intel_gpu/src/plugin/program_builder.cpp b/src/plugins/intel_gpu/src/plugin/program_builder.cpp
index eb7c5bf46fd..6646cee1ca3 100644
--- a/src/plugins/intel_gpu/src/plugin/program_builder.cpp
+++ b/src/plugins/intel_gpu/src/plugin/program_builder.cpp
@@ -244,6 +244,12 @@ std::vector<cldnn::input_info> ProgramBuilder::GetInputInfo(const std::shared_pt
|| ov::is_type<ov::op::v1::Split>(prevOp)
|| ov::is_type<ov::op::v1::VariadicSplit>(prevOp)
|| ov::is_type<ov::op::v4::LSTMCell>(prevOp);
+
+ // Custom op need to maintain output port index for multiple outputs.
+ if (m_custom_layers.find(prevOp->get_type_name()) != m_custom_layers.end()) {
+ is_legacy_multiple_outputs = false;
+ }
+
if (prevOp->get_output_size() > 1 && is_legacy_multiple_outputs) {
prevName += ".out" + std::to_string(op->get_input_source_output(i).get_index());
}
diff --git a/src/plugins/intel_gpu/tests/functional/custom_op/custom_op_dynamic.cpp b/src/plugins/intel_gpu/tests/functional/custom_op/custom_op_dynamic.cpp
index b8c4046f9ec..1a3d206e55f 100644
--- a/src/plugins/intel_gpu/tests/functional/custom_op/custom_op_dynamic.cpp
+++ b/src/plugins/intel_gpu/tests/functional/custom_op/custom_op_dynamic.cpp
@@ -62,6 +62,63 @@ public:
}
};
+class CustomAdd2OutputsOp : public ov::op::Op {
+public:
+ OPENVINO_OP("CustomAdd2OutputsOp", "gpu_opset");
+
+ CustomAdd2OutputsOp() = default;
+
+ CustomAdd2OutputsOp(const ov::OutputVector& inputs) : Op(inputs) {
+ constructor_validate_and_infer_types();
+ }
+
+ void validate_and_infer_types() override {
+ set_output_size(2);
+ set_output_type(0, get_input_element_type(0), get_input_partial_shape(0));
+ set_output_type(1, get_input_element_type(0), get_input_partial_shape(0));
+ }
+
+ bool visit_attributes(ov::AttributeVisitor& visitor) override {
+ return true;
+ }
+
+ std::shared_ptr<ov::Node> clone_with_new_inputs(const ov::OutputVector& new_args) const override {
+ OPENVINO_ASSERT(new_args.size() == 3, "Incorrect number of new arguments");
+ return std::make_shared<CustomAdd2OutputsOp>(new_args);
+ }
+
+ bool has_evaluate() const override {
+ return true;
+ }
+
+ bool evaluate(ov::TensorVector& outputs, const ov::TensorVector& inputs) const override {
+ const float* pBias0 = inputs[1].data<float>();
+ const float* pBias1 = inputs[2].data<float>();
+
+ const auto& in = inputs[0];
+ auto& out0 = outputs[0];
+ auto& out1 = outputs[1];
+
+ out0.set_shape(in.get_shape());
+ out1.set_shape(in.get_shape());
+
+ auto total = in.get_size();
+ if (in.get_element_type() == ov::element::f32) {
+ auto* ptr_in = in.data<float>();
+ auto* ptr_out1 = out0.data<float>();
+ auto* ptr_out2 = out1.data<float>();
+ for (size_t i = 0; i < total; i++) {
+ ptr_out1[i] = ptr_in[i] + pBias0[0];
+ ptr_out2[i] = ptr_in[i] + pBias1[0];
+ }
+ }
+
+ return true;
+ }
+};
+
+const float CONST_BIAS[2] = {0.1f, 0.2f};
+
using CustomOpDynamicTestParams = std::tuple<std::vector<ov::Shape>, // input shape
std::vector<std::vector<float>>>; // input data
class CustomOpDynamic : public ov::test::TestsCommon, public testing::WithParamInterface<CustomOpDynamicTestParams> {
@@ -71,6 +128,7 @@ class CustomOpDynamic : public ov::test::TestsCommon, public testing::WithParamI
void TearDown() override {
ov::test::utils::removeFile(config_cl);
+ ov::test::utils::removeFile(config_cl_2);
ov::test::utils::removeFile(config_xml);
}
@@ -97,46 +155,58 @@ public:
ov::Core core;
float alpha = 1.0, beta = 0.1;
- auto model = generate_model_with_custom_add_op(alpha, beta, ov::PartialShape{-1, dim1, -1});
+ const size_t output_num = 2; // dynamic model has 2 outputs.
+ auto model = generate_model_with_custom_add_op(alpha, beta, ov::PartialShape{-1, dim1, -1}, "dynamic");
ov::AnyMap config = {ov::hint::inference_precision(ov::element::f32), {"CONFIG_FILE", config_xml}};
auto compiled_model = core.compile_model(model, ov::test::utils::DEVICE_GPU, config);
+ check_custom_op_num(compiled_model, output_num);
+
+ auto ireq = compiled_model.create_infer_request();
+ for (size_t i = 0; i < input_datas.size(); i++) {
+ auto input = ov::Tensor({ov::element::f32}, input_shapes[i], input_datas[i].data());
+ ireq.set_input_tensor(0, input);
+ ireq.infer();
+ check_output(ireq, input, alpha, beta, output_num);
+ }
+ }
+
+ void check_custom_op_num(ov::CompiledModel& compiled_model, int expected_output_num) {
auto runtime_graph = compiled_model.get_runtime_model();
auto ops = runtime_graph->get_ordered_ops();
- bool found_custom_op = false;
+ int found_custom_op_num = 0;
for (auto op : ops) {
if (op->get_rt_info()[ov::exec_model_info::LAYER_TYPE].as<std::string>() == "CustomGPUPrimitive") {
- found_custom_op = true;
- break;
+ found_custom_op_num++;
}
}
- ASSERT_TRUE(found_custom_op);
+ ASSERT_EQ(found_custom_op_num, expected_output_num);
+ }
- auto ireq = compiled_model.create_infer_request();
- for (size_t i = 0; i < input_datas.size(); i++) {
- auto input = ov::Tensor({ov::element::f32}, input_shapes[i], input_datas[i].data());
- ireq.set_input_tensor(0, input);
- ireq.infer();
- auto output = ireq.get_output_tensor(0);
+ void check_output(ov::InferRequest ireq, ov::Tensor& input, float alpha, float beta, size_t output_num) {
+ for (size_t j = 0; j < output_num; j++) {
+ auto output = ireq.get_output_tensor(j);
std::vector<float> actual(output.data<float>(), output.data<float>() + output.get_size());
ASSERT_EQ(output.get_element_type(), element::f32);
float* inp_data = input.data<float>();
for (size_t i = 0; i < output.get_size(); i++) {
- ASSERT_FLOAT_EQ(actual[i], inp_data[i] * alpha + beta);
+ ASSERT_FLOAT_EQ(actual[i], inp_data[i] * alpha + beta + (output_num == 1u ? 0 : CONST_BIAS[j]));
}
}
}
protected:
std::string config_cl;
+ std::string config_cl_2;
std::string config_xml;
void generate_config_files() {
config_cl = ov::test::utils::generateTestFilePrefix() + "_custom_op_dynamic.cl";
+ config_cl_2 = ov::test::utils::generateTestFilePrefix() + "_custom_op_dynamic_2.cl";
config_xml = ov::test::utils::generateTestFilePrefix() + "_custom_op_dynamic.xml";
std::string content_cl = R"(
@@ -156,6 +226,21 @@ protected:
outp[dst_index] = inp0[src_index] * alpha + beta;
})";
+ std::string content_cl_2 = R"(
+ __kernel void custom_add_with_2_outputs_kernel(
+ __global const INPUT0_TYPE* inp0,
+ __global const INPUT1_TYPE* inp1,
+ __global const INPUT2_TYPE* inp2,
+ __global OUTPUT0_TYPE* outp0,
+ __global OUTPUT1_TYPE* outp1) {
+ int b = get_global_id(0);
+ int f = get_global_id(1);
+ int y = get_global_id(2);
+ const unsigned id = b*INPUT0_DIMS[1]*INPUT0_DIMS[2]*INPUT0_DIMS[3] + f*INPUT0_DIMS[2]*INPUT0_DIMS[3] + y*INPUT0_DIMS[3];
+ outp0[id] = inp0[id] + inp1[0];
+ outp1[id] = inp0[id] + inp2[0];
+ })";
+
std::string content_xml = R"(
<CustomLayer name="CustomAddOp" type="SimpleGPU" version="1">
<Kernel entry="custom_add_kernel">
@@ -171,15 +256,49 @@ protected:
<WorkSizes global="B,F,Y"/>
</CustomLayer>)";
+ std::string content_xml_2 = R"(
+ <CustomLayer name="CustomAdd2OutputsOp" type="SimpleGPU" version="1">
+ <Kernel entry="custom_add_with_2_outputs_kernel">
+ <Source filename=")" + config_cl_2 + R"("/>
+ </Kernel>
+ <Buffers>
+ <Tensor arg-index="0" type="input" port-index="0" format="BFYX"/>
+ <Tensor arg-index="1" type="input" port-index="1" format="BFYX"/>
+ <Tensor arg-index="2" type="input" port-index="2" format="BFYX"/>
+ <Tensor arg-index="3" type="output" port-index="0" format="BFYX"/>
+ <Tensor arg-index="4" type="output" port-index="1" format="BFYX"/>
+ </Buffers>
+ <CompilerOptions options="-cl-mad-enable"/>
+ <WorkSizes global="B,F,Y"/>
+ </CustomLayer>
+ )";
+
ov::test::utils::createFile(config_cl, content_cl);
- ov::test::utils::createFile(config_xml, content_xml);
+ ov::test::utils::createFile(config_cl_2, content_cl_2);
+ ov::test::utils::createFile(config_xml, content_xml + content_xml_2);
}
- std::shared_ptr<ov::Model> generate_model_with_custom_add_op(float alpha, float beta, ov::PartialShape inp_shape) {
+ std::shared_ptr<ov::Model> generate_model_with_custom_add_op(float alpha, float beta, ov::PartialShape inp_shape, const std::string& model_name_suffix ) {
auto input = std::make_shared<ov::op::v0::Parameter>(ov::element::f32, inp_shape);
- auto op = std::make_shared<CustomAddOp>(input, alpha, beta);
- auto result = std::make_shared<ov::op::v0::Result>(op);
- return std::make_shared<ov::Model>(ov::ResultVector{result}, ov::ParameterVector{input}, "model_with_custom_op_dynamic");
+ auto op_custom_1 = std::make_shared<CustomAddOp>(input, alpha, beta);
+
+ if (model_name_suffix == "static") {
+ auto result = std::make_shared<ov::op::v0::Result>(op_custom_1->output(0));
+ return std::make_shared<ov::Model>(ov::ResultVector{result}, ov::ParameterVector{input}, "model_with_custom_op_" + model_name_suffix);
+ }
+
+ auto const1 = std::make_shared<ov::op::v0::Constant>(ov::element::f32, ov::Shape{1}, std::vector<size_t>{1});
+ const1->set_friendly_name("Const_1");
+ const1->fill_data(ov::element::f32, CONST_BIAS[0]);
+ auto const2 = std::make_shared<ov::op::v0::Constant>(ov::element::f32, ov::Shape{1}, std::vector<size_t>{1});
+ const2->set_friendly_name("Const_2");
+ const2->fill_data(ov::element::f32, CONST_BIAS[1]);
+
+ auto op_custom_2 = std::make_shared<CustomAdd2OutputsOp>(ov::OutputVector{op_custom_1->output(0), const1->output(0), const2->output(0)});
+
+ auto result_1 = std::make_shared<ov::op::v0::Result>(op_custom_2->output(0));
+ auto result_2 = std::make_shared<ov::op::v0::Result>(op_custom_2->output(1));
+ return std::make_shared<ov::Model>(ov::ResultVector{result_1, result_2}, ov::ParameterVector{input}, "model_with_custom_op_" + model_name_suffix);
}
};
@@ -194,36 +313,20 @@ public:
ov::Core core;
float alpha = 1.0, beta = 0.1;
- auto model = generate_model_with_custom_add_op(alpha, beta, ov::PartialShape(input_shapes[0]));
+ const size_t output_num = 1; // static model has 1 outputs.
+ auto model = generate_model_with_custom_add_op(alpha, beta, ov::PartialShape(input_shapes[0]), "static");
ov::AnyMap config = {ov::hint::inference_precision(ov::element::f32), {"CONFIG_FILE", config_xml}};
auto compiled_model = core.compile_model(model, ov::test::utils::DEVICE_GPU, config);
- auto runtime_graph = compiled_model.get_runtime_model();
- auto ops = runtime_graph->get_ordered_ops();
-
- bool found_custom_op = false;
- for (auto op : ops) {
- if (op->get_rt_info()[ov::exec_model_info::LAYER_TYPE].as<std::string>() == "CustomGPUPrimitive") {
- found_custom_op = true;
- break;
- }
- }
- ASSERT_TRUE(found_custom_op);
+ check_custom_op_num(compiled_model, output_num);
auto ireq = compiled_model.create_infer_request();
auto input = ov::Tensor({ov::element::f32}, input_shapes[0], input_datas[0].data());
ireq.set_input_tensor(0, input);
ireq.infer();
- auto output = ireq.get_output_tensor(0);
- std::vector<float> actual(output.data<float>(), output.data<float>() + output.get_size());
-
- ASSERT_EQ(output.get_element_type(), element::f32);
- float* inp_data = input.data<float>();
- for (size_t i = 0; i < output.get_size(); i++) {
- ASSERT_FLOAT_EQ(actual[i], inp_data[i] * alpha + beta);
- }
+ check_output(ireq, input, alpha, beta, output_num);
}
};
diff --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.cpp
index c769473ff5b..b9c920cb1fc 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
@@ -66,7 +66,7 @@ TEST(custom_gpu_primitive_f32, add_basic_in2x2x2x2) {
entry_point,
parameters,
"-cl-mad-enable",
- output_layout,
+ { output_layout },
gws));
set_values(input, {
@@ -171,7 +171,7 @@ void add_basic_in2x2x2x2_with_reorder()
entry_point,
parameters,
"-cl-mad-enable",
- output_layout,
+ { output_layout },
gws));
topology.add(reorder("to_float", input_info("user_kernel"), { data_types::f32, format::yxfb,{ 2, 2, 2, 2 } }));
@@ -272,7 +272,7 @@ TEST(custom_gpu_primitive_f32, eltwise_add_basic_in2x2x2x2) {
entry_point,
parameters,
"-cl-mad-enable",
- output_layout,
+ { output_layout },
gws));
set_values(input, {
@@ -364,7 +364,7 @@ TEST(custom_gpu_primitive_f32, add_eltwise_basic_in2x2x2x2) {
entry_point,
parameters,
"-cl-mad-enable -DSCALAR=1",
- output_layout,
+ { output_layout },
gws));
topology.add(eltwise("eltwise", { input_info("user_kernel"), input_info("input2") }, eltwise_mode::sum));
@@ -464,7 +464,7 @@ TEST(custom_gpu_primitive_f32, two_kernels_with_same_entry_point_basic_in2x2x2x2
entry_point,
parameters,
"-cl-mad-enable -DSCALAR=1",
- output_layout,
+ { output_layout },
gws));
topology.add(custom_gpu_primitive(
"user_kernel2",
@@ -473,7 +473,7 @@ TEST(custom_gpu_primitive_f32, two_kernels_with_same_entry_point_basic_in2x2x2x2
entry_point,
parameters,
"-cl-mad-enable -DSCALAR=3",
- output_layout,
+ { output_layout },
gws));
set_values(input, {
@@ -530,7 +530,7 @@ void test_custom_gpu_primitive_u8_add_basic_in2x2x2x2(bool is_caching_test) {
entry_point,
parameters,
"-cl-mad-enable",
- output_layout,
+ { output_layout },
gws));
set_values<T>(input, {