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] Introduce a parallelized NonZero implementation (#35239)
Mikhail Dvoretckiic31cd6bab4a3
diff --git a/src/plugins/intel_gpu/src/kernel_selector/cl_kernels/gather_nonzero_group.cl b/src/plugins/intel_gpu/src/kernel_selector/cl_kernels/gather_nonzero_group.clnew file mode 100644index 00000000000..93427490f56--- /dev/null+++ b/src/plugins/intel_gpu/src/kernel_selector/cl_kernels/gather_nonzero_group.cl@@ -0,0 +1,187 @@+// Copyright (C) 2018-2026 Intel Corporation+// SPDX-License-Identifier: Apache-2.0+//++#include "include/batch_headers/common.cl"++#define VSIZE 8+#define VLOAD CAT(vload, VSIZE)+#define VSTORE CAT(vstore,VSIZE)+#define OUTPUT_VTYPE MAKE_VECTOR_TYPE(OUTPUT_TYPE, VSIZE)++KERNEL (gather_nonzero_ref)(+ OPTIONAL_SHAPE_INFO_ARG+ const __global INPUT0_TYPE* input,+ volatile __global INPUT1_TYPE* output_shape,+ __global OUTPUT_TYPE* output)+{+ const uint local_idx = get_local_id(0);+ const uint num_work_items = get_global_size(0);+ const uint data_size = TOTAL_DATA_SIZE;+ const uint items_num = data_size / num_work_items;+ const uint leftovers = data_size - (items_num * num_work_items);++ uint workitem_nonzero_count = 0;+ uint actual_items_num = items_num;+ uint input_idx = (actual_items_num * local_idx) + leftovers;+ if (local_idx < leftovers) {+ actual_items_num = items_num + 1;+ input_idx = actual_items_num * local_idx;+ }+ uint final_item = input_idx + actual_items_num;++ for (uint iter = 0; iter < actual_items_num; iter++) {+ const uint idx = input_idx + iter;+ uint count = (input[idx] == INPUT0_VAL_ZERO) ? 0 : 1;+ workitem_nonzero_count += count;+ }++ work_group_barrier(CLK_LOCAL_MEM_FENCE);+ uint local_offset = work_group_scan_exclusive_add(workitem_nonzero_count);++ const int result_size = OV_INPUT_RANK * OUTPUT_FEATURE_NUM; // output shape: [ov_rank, count_nonzero]++ OUTPUT_TYPE* out_mem;+ bool use_local_mem = false;+ __local OUTPUT_TYPE out_mem_slm[MAX_LOCAL_MEM_SIZE];+#if IS_DYNAMIC+ const int dst_slm_size = TOTAL_DATA_SIZE * OV_INPUT_RANK;+ use_local_mem = dst_slm_size < MAX_LOCAL_MEM_SIZE;+#endif // IS_DYNAMIC+#ifdef USE_LOCAL_MEM+ use_local_mem = true;+#endif // USE_LOCAL_MEM+ if (use_local_mem) {+ out_mem = out_mem_slm;+ } else {+ out_mem = output;+ }++ int count_nzero = output_shape[0];+#if OV_INPUT_RANK == 1 // b+ #define ADD_IDXS \+ int b = input_idx_v / INPUT0_BATCH_PITCH; \+ out_mem[local_offset++] = b;+#elif OV_INPUT_RANK == 2 // bf+ #define ADD_IDXS \+ int b = input_idx_v / INPUT0_BATCH_PITCH; \+ int f = (input_idx_v - (b * INPUT0_BATCH_PITCH)) / INPUT0_FEATURE_PITCH; \+ int b_pos = local_offset; \+ int f_pos = b_pos + count_nzero; \+ out_mem[b_pos] = b; \+ out_mem[f_pos] = f; \+ local_offset++;+#elif OV_INPUT_RANK == 3 // bfy+ #define ADD_IDXS \+ int b = input_idx_v / INPUT0_BATCH_PITCH; \+ int f = (input_idx_v - (b * INPUT0_BATCH_PITCH)) / INPUT0_FEATURE_PITCH; \+ int y = (input_idx_v - (b * INPUT0_BATCH_PITCH) - (f * INPUT0_FEATURE_PITCH)) / INPUT0_Y_PITCH; \+ int b_pos = local_offset; \+ int f_pos = b_pos + count_nzero; \+ int y_pos = f_pos + count_nzero; \+ out_mem[b_pos] = b; \+ out_mem[f_pos] = f; \+ out_mem[y_pos] = y; \+ local_offset++;+#elif OV_INPUT_RANK == 4 // bfyx+ #define ADD_IDXS \+ int b = input_idx_v / INPUT0_BATCH_PITCH; \+ int f = (input_idx_v - (b * INPUT0_BATCH_PITCH)) / INPUT0_FEATURE_PITCH; \+ int y = (input_idx_v - (b * INPUT0_BATCH_PITCH) - (f * INPUT0_FEATURE_PITCH)) / INPUT0_Y_PITCH; \+ int x = (input_idx_v - (b * INPUT0_BATCH_PITCH) - (f * INPUT0_FEATURE_PITCH) - (y * INPUT0_Y_PITCH)) / INPUT0_X_PITCH; \+ int b_pos = local_offset; \+ int f_pos = b_pos + count_nzero; \+ int y_pos = f_pos + count_nzero; \+ int x_pos = y_pos + count_nzero; \+ out_mem[b_pos] = b; \+ out_mem[f_pos] = f; \+ out_mem[y_pos] = y; \+ out_mem[x_pos] = x; \+ local_offset++;+#elif OV_INPUT_RANK == 5 // bfzyx+ #define ADD_IDXS \+ int b = input_idx_v / INPUT0_BATCH_PITCH; \+ int f = (input_idx_v - (b * INPUT0_BATCH_PITCH)) / INPUT0_FEATURE_PITCH; \+ int z = (input_idx_v - (b * INPUT0_BATCH_PITCH) - (f * INPUT0_FEATURE_PITCH)) / INPUT0_Z_PITCH; \+ int y = (input_idx_v - (b * INPUT0_BATCH_PITCH) - (f * INPUT0_FEATURE_PITCH) - (z * INPUT0_Z_PITCH)) / INPUT0_Y_PITCH; \+ int x = (input_idx_v - (b * INPUT0_BATCH_PITCH) - (f * INPUT0_FEATURE_PITCH) - (z * INPUT0_Z_PITCH) - (y * INPUT0_Y_PITCH)) / INPUT0_X_PITCH; \+ int b_pos = local_offset; \+ int f_pos = b_pos + count_nzero; \+ int z_pos = f_pos + count_nzero; \+ int y_pos = z_pos + count_nzero; \+ int x_pos = y_pos + count_nzero; \+ out_mem[b_pos] = b; \+ out_mem[f_pos] = f; \+ out_mem[z_pos] = z; \+ out_mem[y_pos] = y; \+ out_mem[x_pos] = x; \+ local_offset++;+#elif OV_INPUT_RANK == 6 // bfwzyx+ #define ADD_IDXS \+ int b = input_idx_v / INPUT0_BATCH_PITCH; \+ int f = (input_idx_v - (b * INPUT0_BATCH_PITCH)) / INPUT0_FEATURE_PITCH; \+ int w = (input_idx_v - (b * INPUT0_BATCH_PITCH) - (f * INPUT0_FEATURE_PITCH)) / INPUT0_W_PITCH; \+ int z = (input_idx_v - (b * INPUT0_BATCH_PITCH) - (f * INPUT0_FEATURE_PITCH) - (w * INPUT0_W_PITCH)) / INPUT0_Z_PITCH; \+ int y = (input_idx_v - (b * INPUT0_BATCH_PITCH) - (f * INPUT0_FEATURE_PITCH) - (w * INPUT0_W_PITCH) - (z * INPUT0_Z_PITCH)) / INPUT0_Y_PITCH; \+ int x = (input_idx_v - (b * INPUT0_BATCH_PITCH) - (f * INPUT0_FEATURE_PITCH) - (w * INPUT0_W_PITCH) - (z * INPUT0_Z_PITCH) - (y * INPUT0_Y_PITCH)) / INPUT0_X_PITCH; \+ int b_pos = local_offset; \+ int f_pos = b_pos + count_nzero; \+ int w_pos = f_pos + count_nzero; \+ int z_pos = w_pos + count_nzero; \+ int y_pos = z_pos + count_nzero; \+ int x_pos = y_pos + count_nzero; \+ out_mem[b_pos] = b; \+ out_mem[f_pos] = f; \+ out_mem[w_pos] = w; \+ out_mem[z_pos] = z; \+ out_mem[y_pos] = y; \+ out_mem[x_pos] = x; \+ local_offset++;+#endif++ // load to local mem+ for (; input_idx + VSIZE <= final_item; input_idx += VSIZE) {+ MAKE_VECTOR_TYPE(INPUT0_TYPE, VSIZE) inputs = VLOAD(0, input + input_idx);+ for (int v = 0; v < VSIZE; ++v) {+ int input_idx_v = input_idx + v;+ if (inputs[v] != INPUT0_VAL_ZERO) {+ ADD_IDXS;+ }+ }+ }++ // leftovers+ for (;input_idx < final_item; ++input_idx) {+ int input_idx_v = input_idx;+ int v = 0;+ if (input[input_idx] != INPUT0_VAL_ZERO) {+ ADD_IDXS;+ }+ }++ work_group_barrier(CLK_LOCAL_MEM_FENCE);+ int global_output_offset = 0;+ if (use_local_mem && local_idx == 0) {+ // write back to global mem+ int local_out_iter = 0;+ for (; local_out_iter + VSIZE < result_size; local_out_iter += VSIZE) {+ VSTORE(VLOAD(0, out_mem + local_out_iter), 0, output + global_output_offset + local_out_iter);+ }+ // leftover+ for (; local_out_iter < result_size; ++local_out_iter) {+ output[global_output_offset + local_out_iter] = out_mem[local_out_iter];+ }+ }+}+#ifdef VLOAD+#undef VLOAD+#endif+#ifdef VSTORE+#undef VSTORE+#endif+#ifdef VSIZE+#undef VSIZE+#endif+#ifdef OUTPUT_VTYPE+#undef OUTPUT_VTYPE+#endifdiff --git a/src/plugins/intel_gpu/src/kernel_selector/kernels/non_zero/gather_nonzero_kernel_group.cpp b/src/plugins/intel_gpu/src/kernel_selector/kernels/non_zero/gather_nonzero_kernel_group.cppnew file mode 100644index 00000000000..deb1e4ddfad--- /dev/null+++ b/src/plugins/intel_gpu/src/kernel_selector/kernels/non_zero/gather_nonzero_kernel_group.cpp@@ -0,0 +1,126 @@+// Copyright (C) 2018-2026 Intel Corporation+// SPDX-License-Identifier: Apache-2.0+//++#include "gather_nonzero_kernel_group.h"+#include "kernel_selector_utils.h"+#include <string>++namespace kernel_selector {+ParamsKey GatherNonzeroKernelGroup::GetSupportedKey() const {+ ParamsKey k;+ k.EnableInputDataType(Datatype::F16);+ k.EnableInputDataType(Datatype::F32);+ k.EnableInputDataType(Datatype::INT8);+ k.EnableInputDataType(Datatype::UINT8);+ k.EnableInputDataType(Datatype::INT32);+ k.EnableInputDataType(Datatype::UINT32);+ k.EnableInputDataType(Datatype::INT64);+ k.EnableOutputDataType(Datatype::F16);+ k.EnableOutputDataType(Datatype::F32);+ k.EnableOutputDataType(Datatype::UINT8);+ k.EnableOutputDataType(Datatype::INT8);+ k.EnableOutputDataType(Datatype::INT32);+ k.EnableOutputDataType(Datatype::UINT32);+ k.EnableOutputDataType(Datatype::INT64);+ k.EnableInputLayout(DataLayout::bfyx);+ k.EnableOutputLayout(DataLayout::bfyx);+ k.EnableInputLayout(DataLayout::bfzyx);+ k.EnableOutputLayout(DataLayout::bfzyx);+ k.EnableInputLayout(DataLayout::bfwzyx);+ k.EnableOutputLayout(DataLayout::bfwzyx);+ k.EnableTensorOffset();+ k.EnableTensorPitches();+ k.EnableBatching();+ k.EnableDifferentTypes();+ k.EnableDynamicShapesSupport();+ return k;+}++JitConstants GatherNonzeroKernelGroup::GetJitConstants(const gather_nonzero_params& params) const {+ JitConstants jit = MakeBaseParamsJitConstants(params);+ const auto& input = params.inputs[0];+ jit.AddConstant(MakeJitConstant("OV_INPUT_RANK", params.ov_input_rank));++ // Reserve 4KiB SLM for work_group_scan_exclusive_add+ auto max_local_mem_size = (params.engineInfo.maxLocalMemSize - 4096) / params.outputs[0].ElementSize();+ jit.AddConstant(MakeJitConstant("MAX_LOCAL_MEM_SIZE", max_local_mem_size));++ if (input.is_dynamic()) {+ DimensionAccessHelperJit dims(input);+ const std::string total_data_size = toVectorMulString({dims.x(), dims.y(), dims.z(), dims.w(), dims.f(), dims.b()});+ jit.AddConstant(MakeJitConstant("TOTAL_DATA_SIZE", total_data_size));+ } else {+ jit.AddConstant(MakeJitConstant("TOTAL_DATA_SIZE", params.inputs[0].LogicalSize()));+ if (params.inputs[0].LogicalSize() * params.ov_input_rank < max_local_mem_size) {+ jit.AddConstant(MakeJitConstant("USE_LOCAL_MEM", 1));+ }+ }+ return jit;+}++CommonDispatchData GatherNonzeroKernelGroup::SetDefault(const gather_nonzero_params& params) const {+ CommonDispatchData dispatchData;+ const auto& input = params.inputs[0];++ // Set 1 work group to avoid synchornization issue for summation of nonzero counting.+ size_t max_dim_size = std::min(input.LogicalSize(), params.engineInfo.maxWorkGroupSize);+ dispatchData.lws = dispatchData.gws = {max_dim_size, 1, 1};++ return dispatchData;+}++void GatherNonzeroKernelGroup::GetUpdateDispatchDataFunc(KernelData& kd) const {+ kd.update_dispatch_data_func = [this](const Params& params, KernelData& kd) {+ const auto& prim_params = static_cast<const gather_nonzero_params&>(params);+ auto dispatchData = SetDefault(prim_params);+ OPENVINO_ASSERT(kd.kernels.size() == 1, "[GPU] Invalid kernels size for update dispatch data func");+ kd.kernels[0].params.workGroups.global = dispatchData.gws;+ kd.kernels[0].params.workGroups.local = dispatchData.lws;+ kd.kernels[0].skip_execution = KernelData::SkipKernelExecution(prim_params);+ };+}++KernelsData GatherNonzeroKernelGroup::GetKernelsData(const Params& params) const {+ assert(params.GetType() == KernelType::GATHER_NONZERO);++ KernelData kd = KernelData::Default<gather_nonzero_params>(params);+ gather_nonzero_params& newParams = *static_cast<gather_nonzero_params*>(kd.params.get());++ auto dispatchData = SetDefault(newParams);+ auto entry_point = GetEntryPoint(kernelName, newParams.layerID, params);+ auto cldnn_jit = GetJitConstants(newParams);+ auto jit = CreateJit(kernelName, cldnn_jit, entry_point);++ auto& kernel = kd.kernels[0];++ GetUpdateDispatchDataFunc(kd);++ FillCLKernelData(kernel,+ dispatchData,+ params.engineInfo,+ kernelName,+ jit,+ entry_point,+ "",+ false,+ false,+ 2,+ GetFusedPrimitiveInputsCount(params),+ 1,+ newParams.is_shape_agnostic);++ return {kd};+}++KernelsPriority GatherNonzeroKernelGroup::GetKernelsPriority(const Params& /*params*/) const {+ return FORCE_PRIORITY_9;+}++bool GatherNonzeroKernelGroup::Validate(const Params& p) const {+ if (!KernelBaseOpenCL::Validate(p))+ DO_NOT_USE_THIS_KERNEL(p.layerID);++ return true;+}+} // namespace kernel_selectordiff --git a/src/plugins/intel_gpu/src/kernel_selector/kernels/non_zero/gather_nonzero_kernel_group.h b/src/plugins/intel_gpu/src/kernel_selector/kernels/non_zero/gather_nonzero_kernel_group.hnew file mode 100644index 00000000000..d5cee31f52b--- /dev/null+++ b/src/plugins/intel_gpu/src/kernel_selector/kernels/non_zero/gather_nonzero_kernel_group.h@@ -0,0 +1,26 @@+// Copyright (C) 2018-2026 Intel Corporation+// SPDX-License-Identifier: Apache-2.0+//++#pragma once++#include "kernel_base_opencl.h"+#include "gather_nonzero_params.h"++namespace kernel_selector {+class GatherNonzeroKernelGroup : public KernelBaseOpenCL {+public:+ GatherNonzeroKernelGroup() : KernelBaseOpenCL("gather_nonzero_group") {}+ virtual ~GatherNonzeroKernelGroup() {}++ virtual JitConstants GetJitConstants(const gather_nonzero_params& params) const;+ virtual CommonDispatchData SetDefault(const gather_nonzero_params& params) const;+ KernelsData GetKernelsData(const Params& params) const override;+ KernelsPriority GetKernelsPriority(const Params& params) const override;+ ParamsKey GetSupportedKey() const override;++protected:+ bool Validate(const Params& pp) const override;+ void GetUpdateDispatchDataFunc(KernelData& kd) const override;+};+} // namespace kernel_selectordiff --git a/src/plugins/intel_gpu/src/kernel_selector/kernels/non_zero/gather_nonzero_kernel_ref.h b/src/plugins/intel_gpu/src/kernel_selector/kernels/non_zero/gather_nonzero_kernel_ref.hindex 13e8b31d531..6f712bd2543 100644--- a/src/plugins/intel_gpu/src/kernel_selector/kernels/non_zero/gather_nonzero_kernel_ref.h+++ b/src/plugins/intel_gpu/src/kernel_selector/kernels/non_zero/gather_nonzero_kernel_ref.h@@ -5,16 +5,9 @@#pragma once#include "kernel_base_opencl.h"+#include "gather_nonzero_params.h"namespace kernel_selector {-////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////-// gather_nonzero_params-////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////-struct gather_nonzero_params : public base_params {- gather_nonzero_params() : base_params(KernelType::GATHER_NONZERO) {}- int32_t ov_input_rank = -1;-};-class GatherNonzeroKernelRef : public KernelBaseOpenCL {public:GatherNonzeroKernelRef() : KernelBaseOpenCL("gather_nonzero_ref") {}diff --git a/src/plugins/intel_gpu/src/kernel_selector/kernels/non_zero/gather_nonzero_kernel_selector.cpp b/src/plugins/intel_gpu/src/kernel_selector/kernels/non_zero/gather_nonzero_kernel_selector.cppindex 3b9140c5488..1c6868a5b04 100644--- a/src/plugins/intel_gpu/src/kernel_selector/kernels/non_zero/gather_nonzero_kernel_selector.cpp+++ b/src/plugins/intel_gpu/src/kernel_selector/kernels/non_zero/gather_nonzero_kernel_selector.cpp@@ -3,11 +3,15 @@//#include "gather_nonzero_kernel_selector.h"+#include "gather_nonzero_kernel_group.h"#include "gather_nonzero_kernel_ref.h"namespace kernel_selector {-gather_nonzero_kernel_selector::gather_nonzero_kernel_selector() { Attach<GatherNonzeroKernelRef>(); }+gather_nonzero_kernel_selector::gather_nonzero_kernel_selector() {+ Attach<GatherNonzeroKernelGroup>();+ Attach<GatherNonzeroKernelRef>();+}KernelsData gather_nonzero_kernel_selector::GetBestKernels(const Params& params) const {return GetNaiveBestKernel(params, KernelType::GATHER_NONZERO);diff --git a/src/plugins/intel_gpu/src/kernel_selector/kernels/non_zero/gather_nonzero_params.h b/src/plugins/intel_gpu/src/kernel_selector/kernels/non_zero/gather_nonzero_params.hnew file mode 100644index 00000000000..ed1e50d3056--- /dev/null+++ b/src/plugins/intel_gpu/src/kernel_selector/kernels/non_zero/gather_nonzero_params.h@@ -0,0 +1,14 @@+// Copyright (C) 2018-2026 Intel Corporation+// SPDX-License-Identifier: Apache-2.0+//++#pragma once++#include "kernel_base_opencl.h"++namespace kernel_selector {+struct gather_nonzero_params : public base_params {+ gather_nonzero_params() : base_params(KernelType::GATHER_NONZERO) {}+ int32_t ov_input_rank = -1;+};+} // namespace kernel_selector