Skip to content

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

[GPU] Use atomic ops for scatter element update reduction (#31231)

Chon Ming Lee1e9eb22b9593
diff --git a/src/plugins/intel_gpu/include/intel_gpu/runtime/kernel_args.hpp b/src/plugins/intel_gpu/include/intel_gpu/runtime/kernel_args.hpp
index abd5262700d..a8da184dece 100644
--- a/src/plugins/intel_gpu/include/intel_gpu/runtime/kernel_args.hpp
+++ b/src/plugins/intel_gpu/include/intel_gpu/runtime/kernel_args.hpp
@@ -66,6 +66,7 @@ struct scalar_desc {
using scalars_desc = std::vector<scalar_desc>;
+using local_memory_args_desc = std::vector<size_t>;
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// ArgumentDescpirtor
@@ -85,7 +86,8 @@ struct argument_desc {
ACTIVATIONS_ZERO_POINTS,
COMPENSATION,
INPUT_OF_FUSED_PRIMITIVE,
- SHAPE_INFO
+ SHAPE_INFO,
+ LOCAL_MEMORY_SIZE
};
Types t;
@@ -102,6 +104,7 @@ struct kernel_arguments_desc {
arguments_desc arguments;
scalars_desc scalars;
std::string layerID;
+ local_memory_args_desc local_memory_args;
};
struct kernel_arguments_data {
@@ -123,6 +126,7 @@ struct kernel_arguments_data {
std::vector<memory::cptr> fused_op_inputs;
const scalars_desc* scalars = nullptr;
+ const local_memory_args_desc* local_memory_args = nullptr;
};
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
diff --git a/src/plugins/intel_gpu/src/graph/impls/ocl/primitive_base.hpp b/src/plugins/intel_gpu/src/graph/impls/ocl/primitive_base.hpp
index 62300417735..a782883dee5 100644
--- a/src/plugins/intel_gpu/src/graph/impls/ocl/primitive_base.hpp
+++ b/src/plugins/intel_gpu/src/graph/impls/ocl/primitive_base.hpp
@@ -215,6 +215,7 @@ protected:
auto args = get_arguments(instance);
args.scalars = &_kernel_data.kernels[kd_idx].params.scalars;
+ args.local_memory_args = &_kernel_data.kernels[kd_idx].params.local_memory_args;
for (const auto& m : instance.get_intermediates_memories()) {
args.intermediates.push_back(m);
diff --git a/src/plugins/intel_gpu/src/kernel_selector/cl_kernels/scatter_elements_update_ref.cl b/src/plugins/intel_gpu/src/kernel_selector/cl_kernels/scatter_elements_update_ref.cl
index 1b11e129787..a450bde586a 100644
--- a/src/plugins/intel_gpu/src/kernel_selector/cl_kernels/scatter_elements_update_ref.cl
+++ b/src/plugins/intel_gpu/src/kernel_selector/cl_kernels/scatter_elements_update_ref.cl
@@ -58,76 +58,140 @@
#error "OUTPUT_DIMS is supposed to be same as INPUT2_DIMS"
#endif
-#ifdef IS_SECOND_ITER // Socond kernel only
- #ifdef REDUCE_MODE
- #define SUM_MODE 1
- #define PROD_MODE 2
- #define MIN_MODE 3
- #define MAX_MODE 4
- #define MEAN_MODE 5
-
- #if USE_INIT_VAL == 0
- #if REDUCE_MODE == SUM_MODE
- #define REDUCTION_NEUTRAL_VALUE INPUT0_VAL_ZERO
- #elif REDUCE_MODE == PROD_MODE
- #define REDUCTION_NEUTRAL_VALUE INPUT0_VAL_ONE
- #elif REDUCE_MODE == MIN_MODE
- #define REDUCTION_NEUTRAL_VALUE INPUT0_VAL_MAX
- #elif REDUCE_MODE == MAX_MODE
- #define REDUCTION_NEUTRAL_VALUE INPUT0_VAL_MIN
- #elif REDUCE_MODE == MEAN_MODE
- #define REDUCTION_NEUTRAL_VALUE INPUT0_VAL_ZERO
- #else
- #error "Invalid REDUCE_MODE value"
- #endif
- #endif
+#ifdef REDUCE_MODE
+ #define SUM_MODE 1
+ #define PROD_MODE 2
+ #define MIN_MODE 3
+ #define MAX_MODE 4
+ #define MEAN_MODE 5
+ #if INPUT2_IS_FP == 1 && INPUT2_TYPE_SIZE == 4
+ #define FP_SCALE 65536.0f
+ #elif INPUT2_IS_FP == 1 && INPUT2_TYPE_SIZE == 2
+ #define FP_SCALE 65504.0f
+ #endif
+ #define FP_SCALE_MAX 2147483648.0f
+ #define FP_SCALE_MIN -FP_SCALE_MAX
+ #define FP_INT_MAX 32767
+ #define FP_INT_MIN -FP_INT_MAX
+ #define FP_INT_ZERO 0
+ #define FP_INT_ONE 1
+
+ #if REDUCE_MODE == SUM_MODE
+ #define REDUCTION_NEUTRAL_VALUE FP_INT_ZERO
+ #elif REDUCE_MODE == PROD_MODE
+ #define REDUCTION_NEUTRAL_VALUE FP_INT_ONE
+ #elif REDUCE_MODE == MIN_MODE
+ #define REDUCTION_NEUTRAL_VALUE FP_INT_MAX
+ #elif REDUCE_MODE == MAX_MODE
+ #define REDUCTION_NEUTRAL_VALUE FP_INT_MIN
+ #elif REDUCE_MODE == MEAN_MODE
+ #define REDUCTION_NEUTRAL_VALUE FP_INT_ZERO
+ #else
+ #error "Invalid REDUCE_MODE value"
+ #endif
- inline INPUT2_TYPE FUNC(reduce)(INPUT2_TYPE a, INPUT2_TYPE b)
- {
- #if REDUCE_MODE == SUM_MODE
- return a + b;
- #elif REDUCE_MODE == PROD_MODE
- return a * b;
- #elif REDUCE_MODE == MIN_MODE
- return MIN(a, b);
- #elif REDUCE_MODE == MAX_MODE
- return MAX(a, b);
- #elif REDUCE_MODE == MEAN_MODE
- return a + b;
+ inline int FUNC(to_fixed_point)(INPUT2_TYPE data_in)
+ {
+ #if INPUT2_IS_FP
+ float scaled = data_in;
+ #if INPUT2_TYPE_SIZE == 2
+ scaled = convert_float(scaled) * FP_SCALE;
+ scaled = clamp(scaled, FP_SCALE_MIN, FP_SCALE_MAX);
+ #elif INPUT2_TYPE_SIZE == 4
+ scaled = scaled * FP_SCALE;
+ scaled = clamp(scaled, FP_SCALE_MIN, FP_SCALE_MAX);
+ #endif
+ return convert_int_rte(scaled);
#else
- #error "Invalid REDUCE_MODE value"
+ return data_in;
#endif
- }
+ }
- inline uint add_count(
- #if COUNT_LENGTH > COUNT_LIMIT
- __global int count_k[], __global int count_v[],
- #else
- __local int count_k[], __local int count_v[],
- #endif
- int idx, uint valid_count)
- {
- for (int i = 0; i < valid_count; ++i) {
- if (count_k[i] == idx) {
- count_v[i] += 1;
- return valid_count;
- }
- }
- count_k[valid_count] = idx;
- count_v[valid_count] += 1;
- return valid_count + 1;
- }
-
- inline int get_count(__local int count_k[], __local int count_v[], int it, int *idx)
- {
- if (count_k[it] != -1) {
- *idx = count_k[it];
- count_k[it] = -1;
- return count_v[it];
- }
- return -1;
- }
+
+ #if REDUCE_MODE < 1 && REDUCE_MODE > 5
+ #error "Invalid REDUCE_MODE value"
#endif
+
+ #if REDUCE_MODE == SUM_MODE
+ #define ATOMIC_REDUCE_OP(addr, val) atomic_fetch_add(addr, val)
+ #elif REDUCE_MODE == MIN_MODE
+ #define ATOMIC_REDUCE_OP(addr, val) atomic_fetch_min(addr, val)
+ #elif REDUCE_MODE == MAX_MODE
+ #define ATOMIC_REDUCE_OP(addr, val) atomic_fetch_max(addr, val)
+ #elif REDUCE_MODE == MEAN_MODE
+ #define ATOMIC_REDUCE_OP(addr, val) atomic_fetch_add(addr, val)
+ #elif REDUCE_MODE == PROD_MODE && INPUT2_IS_FP == 1
+ #define ATOMIC_REDUCE_OP(addr, val) { \
+ int expected_value; \
+ int desired_value; \
+ bool success; \
+ do { \
+ expected_value = atomic_load_explicit ( \
+ addr, \
+ memory_order_acquire, \
+ memory_scope_work_group \
+ ); \
+
+ float expected_f32 = convert_float(expected_value) / FP_SCALE; \
+ float val_f32 = convert_float(val) / FP_SCALE; \
+ float desired_f32 = expected_f32 * val_f32; \
+ desired_value = FUNC_CALL(to_fixed_point)(desired_f32); \
+
+ success = atomic_compare_exchange_weak_explicit ( \
+ addr, \
+ &expected_value,\
+ desired_value, \
+ memory_order_acq_rel, \
+ memory_order_acquire, \
+ memory_scope_work_group \
+ ); \
+ } while (!success); \
+ }
+ #elif REDUCE_MODE == PROD_MODE && INPUT2_IS_FP == 0
+ #define ATOMIC_REDUCE_OP(addr, val) { \
+ int expected_value; \
+ int desired_value; \
+ bool success; \
+ do { \
+ expected_value = atomic_load_explicit ( \
+ addr, \
+ memory_order_acquire, \
+ memory_scope_work_group \
+ ); \
+
+ desired_value = expected_value * val; \
+
+ success = atomic_compare_exchange_weak_explicit ( \
+ addr, \
+ &expected_value,\
+ desired_value, \
+ memory_order_acq_rel, \
+ memory_order_acquire, \
+ memory_scope_work_group \
+ ); \
+ } while (!success); \
+ }
+ #endif
+
+ #define DEFINE_ATOMIC_REDUCE(STORAGE_QUALIFIER, SUFFIX) \
+ inline void FUNC(atomic_reduce##SUFFIX)(volatile STORAGE_QUALIFIER int *ptr, int val) \
+ { \
+ atomic_int *atomic_addr = (atomic_int *)ptr; \
+ ATOMIC_REDUCE_OP(atomic_addr, val); \
+ }
+
+ DEFINE_ATOMIC_REDUCE(__local, _local)
+ DEFINE_ATOMIC_REDUCE(__global, _global)
+
+ #define DEFINE_ATOMIC_COUNT(STORAGE_QUALIFIER, SUFFIX) \
+ inline INPUT1_TYPE FUNC(count_add##SUFFIX)(volatile STORAGE_QUALIFIER int *ptr, int val) \
+ { \
+ atomic_int *atomic_addr = (atomic_int *)ptr; \
+ return atomic_fetch_add(atomic_addr, val); \
+ }
+
+ DEFINE_ATOMIC_COUNT(__local, _local)
+ DEFINE_ATOMIC_COUNT(__global, _global)
#endif
KERNEL(scatter_elements_update_ref)(OPTIONAL_SHAPE_INFO_ARG
@@ -138,12 +202,26 @@ KERNEL(scatter_elements_update_ref)(OPTIONAL_SHAPE_INFO_ARG
#if HAS_FUSED_OPS_DECLS
, FUSED_OPS_DECLS
#endif
+#if REDUCE_MODE != 0
+ , __global INPUT1_TYPE* output_fp
+#if ITER >= 1
+#ifdef NO_LOCAL_MEMORY
+ , __global INPUT1_TYPE* reduction_v
+#else
+ , __local INPUT1_TYPE* reduction_v
+ , __local INPUT1_TYPE* reduction_thread
+#endif
+#if REDUCE_MODE == MEAN_MODE
+ , __global INPUT1_TYPE* count_k
+#endif
+#endif
+#endif
)
{
const uint dim0 = get_global_id(0);
const uint dim1 = get_global_id(1);
const uint dim2 = get_global_id(2);
-#ifndef IS_SECOND_ITER // First kernel
+#if ITER == 0 // First kernel
#if OUTPUT_DIMS == 4
const uint x = dim0;
const uint y = dim1;
@@ -166,116 +244,95 @@ KERNEL(scatter_elements_update_ref)(OPTIONAL_SHAPE_INFO_ARG
const uint input_idx = GET_INPUT_INDEX(ORDER);
const uint output_idx = GET_OUTPUT_INDEX(ORDER);
INPUT0_TYPE val = data[input_idx];
- #if HAS_FUSED_OPS
- FUSED_OPS_FIRST_KERNEL;
- output[output_idx] = TO_OUTPUT_TYPE(FUSED_OPS_RESULT_FIRST_KERNEL);
+
+ #ifndef REDUCE_MODE
+ #if HAS_FUSED_OPS
+ FUSED_OPS_FIRST_KERNEL;
+ output[output_idx] = TO_OUTPUT_TYPE(FUSED_OPS_RESULT_FIRST_KERNEL);
+ #else
+ output[output_idx] = ACTIVATION(val, ACTIVATION_PARAMS);
+ #endif
#else
- output[output_idx] = ACTIVATION(val, ACTIVATION_PARAMS);
+ INPUT1_TYPE val_fp = FUNC_CALL(to_fixed_point)(val);
+ output_fp[output_idx] = val_fp;
#endif
-#else
+
+#elif ITER == 1
#ifdef REDUCE_MODE
- const uint tgz = INPUT2_FEATURE_NUM * INPUT2_BATCH_NUM;
+ uint ORDER;
#if OUTPUT_DIMS == 4
- const uint tgx = INPUT2_SIZE_X;
- const uint tgy = INPUT2_SIZE_Y;
+ x = dim0 % INPUT2_SIZE_X;
+ y = dim0 / INPUT2_SIZE_X;
+ f = dim1 % INPUT2_FEATURE_NUM;
+ b = dim2 % INPUT2_BATCH_NUM;
#elif OUTPUT_DIMS == 5
- const uint tgx = INPUT2_SIZE_X * INPUT2_SIZE_Y;
- const uint tgy = INPUT2_SIZE_Z;
+ x = dim0 % INPUT2_SIZE_X;
+ y = dim0 / INPUT2_SIZE_X;
+ z = dim1 % INPUT2_SIZE_Z;
+ f = dim1 / INPUT2_SIZE_Z;
+ b = dim2 % INPUT2_BATCH_NUM;
#elif OUTPUT_DIMS == 6
- const uint tgx = INPUT2_SIZE_X * INPUT2_SIZE_Y;
- const uint tgy = INPUT2_SIZE_Z * INPUT2_SIZE_W;
+ x = dim0 % INPUT2_SIZE_X;
+ y = dim0 / INPUT2_SIZE_X;
+ z = dim1 % INPUT2_SIZE_Z;
+ w = dim1 / INPUT2_SIZE_Z;
+ f = dim2 % INPUT2_FEATURE_NUM;
+ b = dim2 / INPUT2_FEATURE_NUM;
#endif
- #if COUNT_LENGTH > COUNT_LIMIT
- __global int count_k[COUNT_LENGTH];
- __global int count_v[COUNT_LENGTH];
- #elif COUNT_LENGTH == 0
- __local int count_k[1];
- __local int count_v[1];
- #else
- __local int count_k[COUNT_LENGTH];
- __local int count_v[COUNT_LENGTH];
+ const uint indices_idx = GET_INDICES_INDEX(ORDER);
+ const uint updates_idx = GET_UPDATES_INDEX(ORDER);
+ INPUT2_TYPE val = updates[(int)updates_idx];
+ INPUT1_TYPE index = indices[(int)indices_idx];
+
+ if (index < 0) { index += SIZE; }
+ ASSIGN_INDEX(index);
+ const uint output_idx = GET_OUTPUT_INDEX(ORDER);
+ #ifndef NO_LOCAL_MEMORY
+ reduction_thread[output_idx] = 0;
#endif
- for (int i = 0; i < COUNT_LENGTH; ++i) {
- count_k[i] = -1;
- count_v[i] = 0;
- }
- #if USE_INIT_VAL == 0
- for (uint gz = 0; gz < tgz; gz++) {
- for (uint gy = 0; gy < tgy; gy++) {
- for (uint gx = 0; gx < tgx; gx++) {
- uint ORDER;
- #if OUTPUT_DIMS == 4
- x = gx;
- y = gy;
- #elif OUTPUT_DIMS == 5
- x = gx % INPUT2_SIZE_X;
- y = gx / INPUT2_SIZE_X;
- z = gy;
- #elif OUTPUT_DIMS == 6
- x = gx % INPUT2_SIZE_X;
- y = gx / INPUT2_SIZE_X;
- z = gy % INPUT2_SIZE_Z;
- w = gy / INPUT2_SIZE_Z;
- #endif
- f = gz % INPUT2_FEATURE_NUM;
- b = gz / INPUT2_FEATURE_NUM;
- const uint indices_idx = GET_INDICES_INDEX(ORDER);
- INPUT1_TYPE index = indices[(int)indices_idx];
- if (index < 0) { index += SIZE; }
- ASSIGN_INDEX(index);
- const uint output_idx = GET_OUTPUT_INDEX(ORDER);
- output[output_idx] = REDUCTION_NEUTRAL_VALUE;
- }
- }
- }
+ reduction_v[output_idx] = FUNC_CALL(to_fixed_point)(REDUCTION_NEUTRAL_VALUE);
+
+ #if REDUCE_MODE == MEAN_MODE
+ count_k[output_idx] = INPUT1_VAL_ZERO;
#endif
- uint valid_count = 0;
- for (uint gz = 0; gz < tgz; gz++) {
- for (uint gy = 0; gy < tgy; gy++) {
- for (uint gx = 0; gx < tgx; gx++) {
- uint ORDER;
- #if OUTPUT_DIMS == 4
- x = gx;
- y = gy;
- #elif OUTPUT_DIMS == 5
- x = gx % INPUT2_SIZE_X;
- y = gx / INPUT2_SIZE_X;
- z = gy;
- #elif OUTPUT_DIMS == 6
- x = gx % INPUT2_SIZE_X;
- y = gx / INPUT2_SIZE_X;
- z = gy % INPUT2_SIZE_Z;
- w = gy / INPUT2_SIZE_Z;
- #endif
- f = gz % INPUT2_FEATURE_NUM;
- b = gz / INPUT2_FEATURE_NUM;
- const uint indices_idx = GET_INDICES_INDEX(ORDER);
- const uint updates_idx = GET_UPDATES_INDEX(ORDER);
- INPUT2_TYPE val = updates[(int)updates_idx];
- INPUT1_TYPE index = indices[(int)indices_idx];
- if (index < 0) {index += SIZE;}
- ASSIGN_INDEX(index);
- const uint output_idx = GET_OUTPUT_INDEX(ORDER);
- val = FUNC_CALL(reduce)(output[output_idx], val);
- output[output_idx] = val;
- valid_count = add_count(count_k, count_v, output_idx, valid_count);
- }
- }
- }
- for (int i = 0; i < valid_count; ++i) {
- int output_idx;
- const int count = get_count(count_k, count_v, i, &output_idx);
- #if REDUCE_MODE==MEAN_MODE
- output[output_idx] = output[output_idx] / (count + USE_INIT_VAL);
+
+ barrier(CLK_LOCAL_MEM_FENCE);
+
+ int val_fixed = FUNC_CALL(to_fixed_point)(val);
+
+ #ifndef NO_LOCAL_MEMORY
+ INPUT1_TYPE write_thread = FUNC_CALL(count_add_local)(&reduction_thread[output_idx], 1);
+ if (write_thread == 0) {
+ #if USE_INIT_VAL == 0
+ output_fp[output_idx] = FUNC_CALL(to_fixed_point)(REDUCTION_NEUTRAL_VALUE);
+ #endif
+ }
+ if (reduction_thread[output_idx] > 1) {
+ FUNC_CALL(atomic_reduce_local)(&reduction_v[output_idx], val_fixed);
+ } else {
+ reduction_v[output_idx] = val_fixed;
+ }
+
+ barrier(CLK_LOCAL_MEM_FENCE);
+
+ #if REDUCE_MODE == MEAN_MODE
+ FUNC_CALL(count_add_global)(&count_k[output_idx], INPUT1_VAL_ONE);
#endif
- INPUT2_TYPE val = output[output_idx];
- #if HAS_FUSED_OPS
- FUSED_OPS_SECOND_KERNEL;
- output[output_idx] = TO_OUTPUT_TYPE(FUSED_OPS_RESULT_SECOND_KERNEL);
- #else
- output[output_idx] = ACTIVATION(val, ACTIVATION_PARAMS);
+
+ if (write_thread == 0) {
+ FUNC_CALL(atomic_reduce_global)(&output_fp[output_idx], reduction_v[output_idx]);
+ }
+ #else
+ #if REDUCE_MODE == MEAN_MODE
+ FUNC_CALL(count_add_global)(&count_k[output_idx], INPUT1_VAL_ONE);
#endif
- }
+
+ #if USE_INIT_VAL == 0
+ output_fp[output_idx] = FUNC_CALL(to_fixed_point)(REDUCTION_NEUTRAL_VALUE);
+ #endif
+
+ FUNC_CALL(atomic_reduce_global)(&output_fp[output_idx], val_fixed);
+ #endif
#else // REDUCE_MODE==NONE.
uint ORDER;
#if OUTPUT_DIMS == 4
@@ -311,6 +368,48 @@ KERNEL(scatter_elements_update_ref)(OPTIONAL_SHAPE_INFO_ARG
output[output_idx] = ACTIVATION(val, ACTIVATION_PARAMS);
#endif
#endif
+#elif ITER == 2 && REDUCE_MODE != 0
+ uint ORDER;
+ #if OUTPUT_DIMS == 4
+ x = dim0;
+ y = dim1;
+ f = dim2 % OUTPUT_FEATURE_NUM;
+ b = dim2 / OUTPUT_FEATURE_NUM;
+ #elif OUTPUT_DIMS == 5
+ x = dim0 % OUTPUT_SIZE_X;
+ y = dim0 / OUTPUT_SIZE_X;
+ z = dim1;
+ f = dim2 % OUTPUT_FEATURE_NUM;
+ b = dim2 / OUTPUT_FEATURE_NUM;
+ #elif OUTPUT_DIMS == 6
+ x = dim0 % OUTPUT_SIZE_X;
+ y = dim0 / OUTPUT_SIZE_X;
+ z = dim1 % OUTPUT_SIZE_Z;
+ w = dim1 / OUTPUT_SIZE_Z;
+ f = dim2 % OUTPUT_FEATURE_NUM;
+ b = dim2 / OUTPUT_FEATURE_NUM;
+ #endif
+ const uint input_idx = GET_INPUT_INDEX(ORDER);
+ const uint output_idx = GET_OUTPUT_INDEX(ORDER);
+
+ #if INPUT2_IS_FP == 1
+ float val_f32 = convert_float(output_fp[input_idx]) / FP_SCALE;
+ INPUT2_TYPE val = TO_OUTPUT_TYPE(val_f32);
+ #else
+ INPUT2_TYPE val = output_fp[input_idx];
+ #endif
+
+ #if REDUCE_MODE == MEAN_MODE
+ if (count_k[output_idx] != 0)
+ val /= (count_k[output_idx] + USE_INIT_VAL);
+ #endif
+
+ #if HAS_FUSED_OPS
+ FUSED_OPS_SECOND_KERNEL;
+ output[output_idx] = TO_OUTPUT_TYPE(FUSED_OPS_RESULT_SECOND_KERNEL);
+ #else
+ output[output_idx] = ACTIVATION(val, ACTIVATION_PARAMS);
+ #endif
#endif
}
@@ -321,6 +420,15 @@ KERNEL(scatter_elements_update_ref)(OPTIONAL_SHAPE_INFO_ARG
#undef MAX_MODE
#undef MEAN_MODE
#undef REDUCTION_NEUTRAL_VALUE
+ #undef ATOMIC_REDUCE_OP
+ #undef DEFINE_ATOMIC_REDUCE
+ #undef FP_SCALE
+ #undef FP_SCALE_MAX
+ #undef FP_SCALE_MIN
+ #undef FP_INT_MAX
+ #undef FP_INT_MIN
+ #undef FP_INT_ZERO
+ #undef FP_INT_ONE
#endif
#undef GET_INDICES_INDEX
diff --git a/src/plugins/intel_gpu/src/kernel_selector/kernel_selector_common.cpp b/src/plugins/intel_gpu/src/kernel_selector/kernel_selector_common.cpp
index ff8f7df4c95..f6c1735b46d 100644
--- a/src/plugins/intel_gpu/src/kernel_selector/kernel_selector_common.cpp
+++ b/src/plugins/intel_gpu/src/kernel_selector/kernel_selector_common.cpp
@@ -580,14 +580,22 @@ std::string toString(ReduceMode mode) {
void clKernelData::save(cldnn::BinaryOutputBuffer& ob) const {
ob(params.workGroups.global, params.workGroups.local);
+
ob << params.arguments.size();
for (const auto& arg : params.arguments) {
ob << make_data(&arg.t, sizeof(cldnn::argument_desc::Types)) << arg.index;
}
+
ob << params.scalars.size();
for (const auto& scalar : params.scalars) {
ob << make_data(&scalar.t, sizeof(cldnn::scalar_desc::Types)) << make_data(&scalar.v, sizeof(cldnn::scalar_desc::ValueT));
}
+
+ ob << params.local_memory_args.size();
+ for (const auto& arg : params.local_memory_args) {
+ ob << cldnn::make_data(&arg, sizeof(arg));
+ }
+
ob << params.layerID;
#ifdef ENABLE_ONEDNN_FOR_GPU
ob << micro_kernels.size();
@@ -614,6 +622,13 @@ void clKernelData::load(cldnn::BinaryInputBuffer& ib) {
ib >> make_data(&scalar.t, sizeof(cldnn::scalar_desc::Types)) >> make_data(&scalar.v, sizeof(cldnn::scalar_desc::ValueT));
}
+ typename cldnn::local_memory_args_desc::size_type local_memory_args_size = 0UL;
+ ib >> local_memory_args_size;
+ params.local_memory_args.resize(local_memory_args_size);
+ for (auto& arg : params.local_memory_args) {
+ ib >> cldnn::make_data(&arg, sizeof(arg));
+ }
+
ib >> params.layerID;
#ifdef ENABLE_ONEDNN_FOR_GPU
diff --git a/src/plugins/intel_gpu/src/kernel_selector/kernel_selector_common.h b/src/plugins/intel_gpu/src/kernel_selector/kernel_selector_common.h
index 89ac1ed0669..6a361b415c3 100644
--- a/src/plugins/intel_gpu/src/kernel_selector/kernel_selector_common.h
+++ b/src/plugins/intel_gpu/src/kernel_selector/kernel_selector_common.h
@@ -55,7 +55,6 @@ using ArgumentDescriptor = cldnn::argument_desc;
using Arguments = cldnn::arguments_desc;
using KernelParams = cldnn::kernel_arguments_desc;
-
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// KernelCode
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
diff --git a/src/plugins/intel_gpu/src/kernel_selector/kernels/scatter_update/scatter_elements_update_kernel_ref.cpp b/src/plugins/intel_gpu/src/kernel_selector/kernels/scatter_update/scatter_elements_update_kernel_ref.cpp
index a4a9a4b4823..4cbb00b1ebf 100644
--- a/src/plugins/intel_gpu/src/kernel_selector/kernels/scatter_update/scatter_elements_update_kernel_ref.cpp
+++ b/src/plugins/intel_gpu/src/kernel_selector/kernels/scatter_update/scatter_elements_update_kernel_ref.cpp
@@ -87,13 +87,6 @@ static inline std::vector<std::string> GetDefaultOrder(size_t size) {
CommonDispatchData ScatterElementsUpdateKernelRef::SetDefault(const scatter_elements_update_params& params, bool is_second) const {
CommonDispatchData dispatchData;
- KernelData kd = KernelData::Default<scatter_elements_update_params>(params, 2);
- if (is_second && params.mode != ScatterUpdateReduction::NONE) {
- dispatchData.gws = {1, 1, 1};
- dispatchData.lws = {1, 1, 1};
- return dispatchData;
- }
-
auto in_layout = params.inputs[0].GetLayout();
auto out_layout = params.outputs[0].GetLayout();
std::vector<std::vector<Tensor::DataChannelName>> dims_by_gws;
@@ -103,33 +96,55 @@ CommonDispatchData ScatterElementsUpdateKernelRef::SetDefault(const scatter_elem
const auto& scope = is_second ? indices : output;
const auto rank = params.inputs[0].GetDims().size();
- switch (rank) {
- case 4:
- dispatchData.gws = {scope.X().v, scope.Y().v, scope.Feature().v * scope.Batch().v};
- dims_by_gws = {{Tensor::DataChannelName::X},
- {Tensor::DataChannelName::Y},
- {Tensor::DataChannelName::FEATURE, Tensor::DataChannelName::BATCH}};
- break;
-
- case 5:
- dispatchData.gws = {scope.X().v * scope.Y().v, scope.Z().v, scope.Feature().v * scope.Batch().v};
- dims_by_gws = {{Tensor::DataChannelName::X, Tensor::DataChannelName::Y},
- {Tensor::DataChannelName::Z},
- {Tensor::DataChannelName::FEATURE, Tensor::DataChannelName::BATCH}};
- break;
-
- case 6:
- dispatchData.gws = {scope.X().v * scope.Y().v, scope.Z().v * scope.W().v, scope.Feature().v * scope.Batch().v};
- dims_by_gws = {{Tensor::DataChannelName::X, Tensor::DataChannelName::Y},
- {Tensor::DataChannelName::Z, Tensor::DataChannelName::W},
- {Tensor::DataChannelName::FEATURE, Tensor::DataChannelName::BATCH}};
- break;
- default:
- throw std::invalid_argument("Unsupported data layout for scatter elements update primitive");
- break;
- }
-
- dispatchData.lws = GetOptimalLocalWorkGroupSizes(dispatchData.gws, params.engineInfo, in_layout, out_layout, dims_by_gws);
+ if (!scope.is_dynamic()) {
+ if (is_second && params.mode != ScatterUpdateReduction::NONE) {
+ switch (rank) {
+ case 4:
+ dispatchData.gws = {indices.X().v * indices.Y().v, indices.Feature().v, indices.Batch().v};
+ dispatchData.lws = {1, 1, indices.Batch().v};
+ break;
+ case 5:
+ dispatchData.gws = {indices.X().v * indices.Y().v, indices.Z().v * indices.Feature().v, indices.Batch().v};
+ dispatchData.lws = {1, 1, indices.Batch().v};
+ break;
+ case 6:
+ dispatchData.gws = {indices.X().v * indices.Y().v, indices.Z().v * indices.W().v, indices.Feature().v * indices.Batch().v};
+ dispatchData.lws = {1, 1, indices.Batch().v};
+ break;
+ default:
+ throw std::invalid_argument("Unsupported data layout for scatter elements update primitive");
+ break;
+ }
+ } else {
+ switch (rank) {
+ case 4:
+ dispatchData.gws = {scope.X().v, scope.Y().v, scope.Feature().v * scope.Batch().v};
+ dims_by_gws = {{Tensor::DataChannelName::X},
+ {Tensor::DataChannelName::Y},
+ {Tensor::DataChannelName::FEATURE, Tensor::DataChannelName::BATCH}};
+ break;
+
+ case 5:
+ dispatchData.gws = {scope.X().v * scope.Y().v, scope.Z().v, scope.Feature().v * scope.Batch().v};
+ dims_by_gws = {{Tensor::DataChannelName::X, Tensor::DataChannelName::Y},
+ {Tensor::DataChannelName::Z},
+ {Tensor::DataChannelName::FEATURE, Tensor::DataChannelName::BATCH}};
+ break;
+
+ case 6:
+ dispatchData.gws = {scope.X().v * scope.Y().v, scope.Z().v * scope.W().v, scope.Feature().v * scope.Batch().v};
+ dims_by_gws = {{Tensor::DataChannelName::X, Tensor::DataChannelName::Y},
+ {Tensor::DataChannelName::Z, Tensor::DataChannelName::W},
+ {Tensor::DataChannelName::FEATURE, Tensor::DataChannelName::BATCH}};
+ break;
+ default:
+ throw std::invalid_argument("Unsupported data layout for scatter elements update primitive");
+ break;
+ }
+ dispatchData.lws = GetOptimalLocalWorkGroupSizes(dispatchData.gws, params.engineInfo, in_layout, out_layout, dims_by_gws);
+ }
+ }
+
return dispatchData;
}
@@ -138,6 +153,11 @@ JitConstants ScatterElementsUpdateKernelRef::GetJitConstants(const scatter_eleme
jit.AddConstant(MakeJitConstant("AXIS_VALUE", GetScatterElementsUpdateChannelIndex(params)));
+ const auto& output = params.outputs[0];
+ if (output.PhysicalSizeInBytes() * 4 > params.engineInfo.maxLocalMemSize) {
+ jit.AddConstant(MakeJitConstant("NO_LOCAL_MEMORY", 1));
+ }
+
if (params.mode != ScatterUpdateReduction::NONE) {
jit.AddConstant(MakeJitConstant("REDUCE_MODE", static_cast<int>(params.mode)));
jit.AddConstant(MakeJitConstant("USE_INIT_VAL", params.use_init_val));
@@ -179,13 +199,41 @@ bool ScatterElementsUpdateKernelRef::SkipKernelExecution(const scatter_elements_
void ScatterElementsUpdateKernelRef::GetUpdateDispatchDataFunc(KernelData& kd) const {
kd.update_dispatch_data_func = [this](const Params& params, KernelData& kd) {
const auto& prim_params = static_cast<const scatter_elements_update_params&>(params);
- OPENVINO_ASSERT(kd.kernels.size() == 2, "[GPU] Invalid kernels size for update dispatch data func");
- for (size_t i = 0; i < 2; ++i) {
+ if (prim_params.mode == ScatterUpdateReduction::NONE) {
+ OPENVINO_ASSERT(kd.kernels.size() == 2, "[GPU] Invalid kernels size for update dispatch data func");
+ } else {
+ OPENVINO_ASSERT(kd.kernels.size() == 3, "[GPU] Invalid kernels size for update dispatch data func");
+ }
+
+ const auto& output = prim_params.outputs[0];
+ const bool use_local_memory = (output.PhysicalSizeInBytes() * 4 > params.engineInfo.maxLocalMemSize) ? false : true;
+
+ if (prim_params.mode != ScatterUpdateReduction::NONE) {
+ kd.internalBuffers.clear();
+ kd.internalBuffers.push_back(output.PhysicalSizeInBytes() * 2);
+
+ if (!use_local_memory) {
+ kd.internalBuffers.push_back(output.PhysicalSizeInBytes() * 2);
+ }
+ if (prim_params.mode == ScatterUpdateReduction::MEAN) {
+ kd.internalBuffers.push_back(output.PhysicalSizeInBytes() * 2);
+ }
+ kd.internalBufferDataType = Datatype::INT32;
+ }
+
+ for (size_t i = 0; i < kd.kernels.size(); ++i) {
auto dispatchData = SetDefault(prim_params, i == 1);
kd.kernels[i].params.workGroups.global = dispatchData.gws;
kd.kernels[i].params.workGroups.local = dispatchData.lws;
kd.kernels[i].skip_execution = SkipKernelExecution(prim_params, i);
+
+ if (i >= 1 && prim_params.mode != ScatterUpdateReduction::NONE && use_local_memory) {
+ const auto& output = prim_params.outputs[0];
+ const auto buffer_size = output.PhysicalSizeInBytes() * 2;
+ kd.kernels[i].params.local_memory_args.clear();
+ kd.kernels[i].params.local_memory_args.push_back(buffer_size);
+ }
}
};
}
@@ -195,27 +243,72 @@ KernelsData ScatterElementsUpdateKernelRef::GetKernelsData(const Params& params)
return {};
}
- KernelData kd = KernelData::Default<scatter_elements_update_params>(params, 2);
+ const auto& prim_params = static_cast<const scatter_elements_update_params&>(params);
+ int kernel_size = 2;
+
+ if (prim_params.mode != ScatterUpdateReduction::NONE) {
+ kernel_size += 1;
+ }
+
+ KernelData kd = KernelData::Default<scatter_elements_update_params>(params, kernel_size);
scatter_elements_update_params& newParams = *static_cast<scatter_elements_update_params*>(kd.params.get());
auto cldnn_jit = GetJitConstants(newParams);
GetUpdateDispatchDataFunc(kd);
- for (int i = 0; i < 2; i++) {
+ const auto& output = newParams.outputs[0];
+ const bool use_local_memory = (output.PhysicalSizeInBytes() * 4 > params.engineInfo.maxLocalMemSize) ? false : true;
+ kd.internalBuffers.clear();
+ kd.internalBuffers.push_back(output.PhysicalSizeInBytes() * 2); // fixed point output
+ if (!use_local_memory) {
+ kd.internalBuffers.push_back(output.PhysicalSizeInBytes() * 2); // reduction value output
+ kd.internalBuffers.push_back(output.PhysicalSizeInBytes() * 2); // reduction_thread_count output
+ }
+ kd.internalBufferDataType = Datatype::INT32;
+ if (newParams.mode == ScatterUpdateReduction::MEAN) {
+ kd.internalBuffers.push_back(output.PhysicalSizeInBytes() * 2); // calculate mean
+ }
+
+ for (int i = 0; i < kernel_size; i++) {
auto dispatchData = SetDefault(newParams, (i == 1));
auto entry_point = GetEntryPoint(kernelName, newParams.layerID, params, i);
+ clKernelData& kernel = kd.kernels[i];
+
+ cldnn_jit.RemoveConstant("ITER");
+ cldnn_jit.AddConstant(MakeJitConstant("ITER", i));
- if (i == 1) {
- cldnn_jit.AddConstant(MakeJitConstant("IS_SECOND_ITER", "true"));
- cldnn_jit.AddConstant(MakeJitConstant("COUNT_LIMIT", params.engineInfo.maxLocalMemSize));
- cldnn_jit.AddConstant(MakeJitConstant("COUNT_LENGTH", newParams.inputs[1].LogicalSize()));
- }
auto jit = CreateJit(kernelName, cldnn_jit, entry_point);
- clKernelData& kernel = kd.kernels[i];
+ if (i >= 1 && newParams.mode != ScatterUpdateReduction::NONE && use_local_memory) {
+ const auto buffer_size = output.PhysicalSizeInBytes() * 2;
+ kd.kernels[i].params.local_memory_args.clear();
+ kd.kernels[i].params.local_memory_args.push_back(buffer_size);
+ }
FillCLKernelData(kernel, dispatchData, params.engineInfo, kernelName, jit, entry_point, "", false, false, 3, GetFusedPrimitiveInputsCount(params), 1,
params.is_shape_agnostic);
+
+ uint32_t buf_idx = 0;
+
+ if (newParams.mode != ScatterUpdateReduction::NONE) {
+ // store output in fixed point
+ kernel.params.arguments.push_back({ArgumentDescriptor::Types::INTERNAL_BUFFER, buf_idx++});
+ }
+
+
+ if (i >= 1 && newParams.mode != ScatterUpdateReduction::NONE) {
+ if (use_local_memory) {
+ // data reduction
+ kernel.params.arguments.push_back({ArgumentDescriptor::Types::LOCAL_MEMORY_SIZE, 0});
+ kernel.params.arguments.push_back({ArgumentDescriptor::Types::LOCAL_MEMORY_SIZE, 0});
+ } else {
+ // identify thread for perform write
+ kernel.params.arguments.push_back({ArgumentDescriptor::Types::INTERNAL_BUFFER, buf_idx++});
+ }
+ if (newParams.mode == ScatterUpdateReduction::MEAN) {
+ kernel.params.arguments.push_back({ArgumentDescriptor::Types::INTERNAL_BUFFER, buf_idx++});
+ }
+ }
}
return {kd};
diff --git a/src/plugins/intel_gpu/src/plugin/transformations_pipeline.cpp b/src/plugins/intel_gpu/src/plugin/transformations_pipeline.cpp
index 7a561d1a02b..1ecd08ff5b8 100644
--- a/src/plugins/intel_gpu/src/plugin/transformations_pipeline.cpp
+++ b/src/plugins/intel_gpu/src/plugin/transformations_pipeline.cpp
@@ -194,7 +194,7 @@ namespace {
template<typename T>
static bool disable_reduce_decomposition(const std::shared_ptr<const ov::Node> node) {
if (auto op = std::dynamic_pointer_cast<const T>(node)) {
- if (op->input(0).get_partial_shape()[0].is_static()) {
+ if (op->input(0).get_partial_shape().is_static()) {
bool fp16_batch_not_1 = op->get_element_type() == ov::element::f16 && op->input(0).get_partial_shape()[0] != 1;
return !fp16_batch_not_1;
}
diff --git a/src/plugins/intel_gpu/src/runtime/ocl/ocl_stream.cpp b/src/plugins/intel_gpu/src/runtime/ocl/ocl_stream.cpp
index 0d972e82453..2b01c511f4a 100644
--- a/src/plugins/intel_gpu/src/runtime/ocl/ocl_stream.cpp
+++ b/src/plugins/intel_gpu/src/runtime/ocl/ocl_stream.cpp
@@ -47,6 +47,15 @@ inline cl::NDRange toNDRange(const std::vector<size_t>& v) {
}
}
+
+cl_int set_kernel_arg(ocl_kernel_type& kernel, uint32_t idx, uint32_t size) {
+ if (size == 0)
+ return CL_INVALID_ARG_VALUE;
+
+ GPU_DEBUG_TRACE_DETAIL << "kernel: " << kernel.get() << " set arg " << idx << " local memory size : " << size << std::endl;
+ return kernel.setArg(idx, size, NULL);
+}
+
cl_int set_kernel_arg(ocl_kernel_type& kernel, uint32_t idx, cldnn::memory::cptr mem) {
if (!mem)
return CL_INVALID_ARG_VALUE;
@@ -174,6 +183,12 @@ void set_arguments_impl(ocl_kernel_type& kernel,
case args_t::SHAPE_INFO:
status = set_kernel_arg(kernel, i, data.shape_info);
break;
+ case args_t::LOCAL_MEMORY_SIZE:
+ OPENVINO_ASSERT(args[i].index < data.local_memory_args->size() && data.local_memory_args->at(args[i].index),
+ "The allocated local memory is necessary to set kernel arguments.");
+ status = set_kernel_arg(kernel, i, data.local_memory_args->at(args[i].index));
+ break;
+ break;
default:
break;
}
diff --git a/src/plugins/intel_gpu/tests/unit/test_cases/scatter_elements_update_gpu_test.cpp b/src/plugins/intel_gpu/tests/unit/test_cases/scatter_elements_update_gpu_test.cpp
index 3f7c16313ae..6fb0ef50d21 100644
--- a/src/plugins/intel_gpu/tests/unit/test_cases/scatter_elements_update_gpu_test.cpp
+++ b/src/plugins/intel_gpu/tests/unit/test_cases/scatter_elements_update_gpu_test.cpp
@@ -561,9 +561,14 @@ INSTANTIATE_TEST_SUITE_P(scatter_elements_update_gpu_formats_test_mixed_inputs,
),
PrintToStringParamName());
+using scatter_elements_update_gpu_reduction_test_f16 = scatter_elements_update_gpu_reduction_test<ov::float16, int32_t>;
using scatter_elements_update_gpu_reduction_test_f32 = scatter_elements_update_gpu_reduction_test<float, int32_t>;
using scatter_elements_update_gpu_reduction_test_i32 = scatter_elements_update_gpu_reduction_test<int32_t, int32_t>;
+TEST_P(scatter_elements_update_gpu_reduction_test_f16, basic) {
+ ASSERT_NO_FATAL_FAILURE(test(false));
+}
+
TEST_P(scatter_elements_update_gpu_reduction_test_f32, basic) {
ASSERT_NO_FATAL_FAILURE(test(false));
}
@@ -580,6 +585,15 @@ const std::vector<ov::op::v12::ScatterElementsUpdate::Reduction> reduce_modes{
ov::op::v12::ScatterElementsUpdate::Reduction::MEAN
};
+INSTANTIATE_TEST_SUITE_P(scatter_elements_update_gpu_reduction_test_f16_2d,
+ scatter_elements_update_gpu_reduction_test_f16,
+ ::testing::Combine(
+ ::testing::ValuesIn(generateScatterElementsUpdateParams2D<ov::float16, int32_t>()),
+ ::testing::ValuesIn(reduce_modes),
+ ::testing::ValuesIn({true, false}),
+ ::testing::Values(format::bfyx)
+ ),
+ PrintToStringParamName());
INSTANTIATE_TEST_SUITE_P(scatter_elements_update_gpu_reduction_test_f32_2d,
scatter_elements_update_gpu_reduction_test_f32,
@@ -601,6 +615,16 @@ INSTANTIATE_TEST_SUITE_P(scatter_elements_update_gpu_reduction_test_i32_2d,
),
PrintToStringParamName());
+INSTANTIATE_TEST_SUITE_P(scatter_elements_update_gpu_reduction_test_f16_3d,
+ scatter_elements_update_gpu_reduction_test_f16,
+ ::testing::Combine(
+ ::testing::ValuesIn(generateScatterElementsUpdateParams3D<ov::float16, int32_t>()),
+ ::testing::ValuesIn(reduce_modes),
+ ::testing::ValuesIn({true, false}),
+ ::testing::Values(format::bfzyx)
+ ),
+ PrintToStringParamName());
+
INSTANTIATE_TEST_SUITE_P(scatter_elements_update_gpu_reduction_test_f32_3d,
scatter_elements_update_gpu_reduction_test_f32,
::testing::Combine(
@@ -611,6 +635,16 @@ INSTANTIATE_TEST_SUITE_P(scatter_elements_update_gpu_reduction_test_f32_3d,
),
PrintToStringParamName());
+INSTANTIATE_TEST_SUITE_P(scatter_elements_update_gpu_reduction_test_f16_4d,
+ scatter_elements_update_gpu_reduction_test_f16,
+ ::testing::Combine(
+ ::testing::ValuesIn(generateScatterElementsUpdateParams4D<ov::float16, int32_t>()),
+ ::testing::ValuesIn(reduce_modes),
+ ::testing::ValuesIn({true, false}),
+ ::testing::Values(format::bfwzyx)
+ ),
+ PrintToStringParamName());
+
INSTANTIATE_TEST_SUITE_P(scatter_elements_update_gpu_reduction_test_f32_4d,
scatter_elements_update_gpu_reduction_test_f32,
::testing::Combine(