Skip to content

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

[GPU] Swiglu OPT kernel implementation (#26172)

Damian Kurek1c2cd12c3472
diff --git a/src/plugins/intel_gpu/src/kernel_selector/cl_kernels/swiglu_gpu_opt.cl b/src/plugins/intel_gpu/src/kernel_selector/cl_kernels/swiglu_gpu_opt.cl
new file mode 100644
index 00000000000..6a019091d06
--- /dev/null
+++ b/src/plugins/intel_gpu/src/kernel_selector/cl_kernels/swiglu_gpu_opt.cl
@@ -0,0 +1,73 @@
+// Copyright (C) 2024 Intel Corporation
+// SPDX-License-Identifier: Apache-2.0
+//
+
+float FUNC(fast_erf)(float x) {
+ // If x is very big just straight up assume the result is +-1.0f
+ if(x > 4.0f) return 1.0f;
+ if(x < -4.0f) return -1.0f;
+
+ float z = fabs(x);
+ // Use Taylor expansion when x is close to 0
+ if(z < 0.44593f) { // Cutoff where Taylor expansion is more precise than A&S formula
+ // Taylor expansion with 5 terms
+ const float a1 = 1.1283791670955126f;
+ const float x2 = x * x;
+ const float x3 = x2 * x;
+ const float x5 = x3 * x2;
+ const float x7 = x5 * x2;
+ const float x9 = x7 * x2;
+
+ return a1 * (x - (x3 / 3.0f) + (x5 / 10.0f) - (x7 / 42.0f) + (x9 / 216.0f));
+ }
+ // A&S formula has a high relative error when x is close to 0
+ else {
+ // From A&S Handbook of Mathematical Functions with Formulas, Graphs, and Mathematical Tables
+ // Formula section 7.1.26
+
+ const float a1 = 0.254829592f;
+ const float a2 = -0.284496736f;
+ const float a3 = 1.421413741f;
+ const float a4 = -1.453152027f;
+ const float a5 = 1.061405429f;
+ const float p = 0.3275911f;
+
+ int sign = (x >= 0) ? 1 : -1;
+
+ float t = 1.0f / (1.0f + p * z);
+ float y = 1.0f - (((((a5 * t + a4) * t) + a3) * t + a2) * t + a1) * t * native_exp(-(z * z));
+
+ return y * sign; // A&S formula is only good for when x >= 0, however -erf(x) = erf(-x)
+ }
+}
+
+#if !IS_DYNAMIC
+__attribute__((reqd_work_group_size(LWS0, LWS1, LWS2)))
+#endif
+KERNEL(swiglu_gpu_opt)(
+ OPTIONAL_SHAPE_INFO_ARG
+ const __global INPUT0_TYPE* input,
+ __global OUTPUT_TYPE* output)
+{
+ const unsigned int x = (uint)get_global_linear_id();
+ const unsigned int y = x + ((x / SPLIT_LENGTH) * SPLIT_LENGTH);
+
+#if SPLIT_TO_GLU_IDX == 0
+ ACCUMULATOR_TYPE gate = input[y];
+ ACCUMULATOR_TYPE value = input[y + SPLIT_LENGTH];
+#else
+ ACCUMULATOR_TYPE gate = input[y + SPLIT_LENGTH];
+ ACCUMULATOR_TYPE value = input[y];
+#endif
+
+ #if GLU_TYPE == 0 // Swish
+ gate /= ACCUMULATOR_VAL_ONE + native_exp(-(ACCUMULATOR_VAL_ONE * gate));
+ #elif GLU_TYPE == 1 // Gelu
+ gate = (GEGLU_HALF * gate * (ACCUMULATOR_VAL_ONE + (FUNC_CALL(fast_erf)(gate * GEGLU_MULT))));
+ #elif GLU_TYPE == 2 // Gelu_Tanh
+ gate = (GEGLU_HALF * gate * (ACCUMULATOR_VAL_ONE + (tanh(GEGLU_SQUARE_2_OVER_PI * gate * (ACCUMULATOR_VAL_ONE + GEGLU_MULT * gate * gate)))));
+ #endif
+
+ value *= gate;
+ output[x] = TO_OUTPUT_TYPE(value);
+}
diff --git a/src/plugins/intel_gpu/src/kernel_selector/kernels/swiglu/swiglu_kernel_base.cpp b/src/plugins/intel_gpu/src/kernel_selector/kernels/swiglu/swiglu_kernel_base.cpp
new file mode 100644
index 00000000000..05ee01adb3f
--- /dev/null
+++ b/src/plugins/intel_gpu/src/kernel_selector/kernels/swiglu/swiglu_kernel_base.cpp
@@ -0,0 +1,107 @@
+// Copyright (C) 2018-2024 Intel Corporation
+// SPDX-License-Identifier: Apache-2.0
+//
+
+#include "swiglu_kernel_base.h"
+#include "kernel_selector_utils.h"
+
+namespace kernel_selector {
+CommonDispatchData SwiGLUKernelBase::SetDefault(const swiglu_params& params) const {
+ CommonDispatchData dispatchData;
+
+ dispatchData.gws = GetTensorFriendlyWorkGroups(params.outputs[0]);
+ dispatchData.lws = GetOptimalLocalWorkGroupSizes(dispatchData.gws, params.engineInfo);
+
+ return dispatchData;
+}
+
+JitConstants SwiGLUKernelBase::GetJitConstants(const swiglu_params& params, const CommonDispatchData& dispatchData) const {
+ JitConstants jit = MakeBaseParamsJitConstants(params);
+
+ jit.AddConstants({MakeJitConstant("AXIS", params.axis)});
+ jit.AddConstants({MakeJitConstant("SPLIT_LENGTH", params.split_length)});
+ jit.AddConstants({MakeJitConstant("GLU_TYPE", params.glu_type)});
+ jit.AddConstants({MakeJitConstant("LWS0", dispatchData.lws[0])});
+ jit.AddConstants({MakeJitConstant("LWS1", dispatchData.lws[1])});
+ jit.AddConstants({MakeJitConstant("LWS2", dispatchData.lws[2])});
+ const std::string type_suffix = (GetAccumulatorType(params) == Datatype::F32) ? "f" : "h";
+ if (params.glu_type == ov::intel_gpu::op::SwiGLU::GluType::Gelu) {
+ jit.AddConstants({MakeJitConstant("GEGLU_HALF", "0.5" + type_suffix)});
+ jit.AddConstants({MakeJitConstant("GEGLU_MULT", "0.7071067811865475" + type_suffix)});
+ } else if (params.glu_type == ov::intel_gpu::op::SwiGLU::GluType::Gelu_Tanh) {
+ jit.AddConstants({MakeJitConstant("GEGLU_HALF", "0.5" + type_suffix)});
+ jit.AddConstants({MakeJitConstant("GEGLU_MULT", "0.044715" + type_suffix)});
+ jit.AddConstants({MakeJitConstant("GEGLU_SQUARE_2_OVER_PI", "0.79788458347320556640625" + type_suffix)});
+ }
+ jit.AddConstants({MakeJitConstant("SPLIT_TO_GLU_IDX", params.split_to_glu_idx)});
+ jit.Merge(MakeTypeJitConstants(GetAccumulatorType(params), "ACCUMULATOR"));
+ jit.Merge(GetTensorFriendlyWorkGroupsJit(params.outputs[0]));
+
+ return jit;
+}
+
+KernelsData SwiGLUKernelBase::GetKernelsData(const Params& params) const {
+ assert(params.GetType() == KernelType::SWIGLU);
+
+ if (!Validate(params))
+ return {};
+
+ const swiglu_params& orgParams = static_cast<const swiglu_params&>(params);
+ auto dispatchData = SetDefault(orgParams);
+
+ KernelData kd = KernelData::Default<swiglu_params>(params);
+
+ auto cldnn_jit = GetJitConstants(orgParams, dispatchData);
+ auto entry_point = GetEntryPoint(kernelName, orgParams.layerID, params);
+ auto jit = CreateJit(kernelName, cldnn_jit, entry_point);
+
+ GetUpdateDispatchDataFunc(kd);
+
+ auto& kernel = kd.kernels[0];
+ FillCLKernelData(kernel,
+ dispatchData,
+ params.engineInfo,
+ kernelName,
+ jit,
+ entry_point,
+ EXE_MODE_DEFAULT,
+ false,
+ false,
+ 1,
+ GetFusedPrimitiveInputsCount(params),
+ 1,
+ orgParams.is_shape_agnostic);
+
+ return {kd};
+}
+
+
+bool SwiGLUKernelBase::Validate(const Params& params) const {
+ if (!KernelBaseOpenCL::Validate(params))
+ return false;
+
+ return true;
+}
+
+Datatype SwiGLUKernelBase::GetAccumulatorType(const swiglu_params& params) const {
+ Datatype types[] = { Datatype::F32, Datatype::F16, Datatype::INT64, Datatype::INT32, Datatype::UINT32};
+
+ for (Datatype type : types)
+ for (auto& in : params.inputs)
+ if (in.GetDType() == type)
+ return type;
+
+ return Datatype::F32;
+}
+
+void SwiGLUKernelBase::GetUpdateDispatchDataFunc(KernelData& kd) const {
+ kd.update_dispatch_data_func = [this](const Params& params, KernelData& kd) {
+ const auto& prim_params = static_cast<const swiglu_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);
+ };
+}
+} // namespace kernel_selector
diff --git a/src/plugins/intel_gpu/src/kernel_selector/kernels/swiglu/swiglu_kernel_base.h b/src/plugins/intel_gpu/src/kernel_selector/kernels/swiglu/swiglu_kernel_base.h
new file mode 100644
index 00000000000..e59baeb9435
--- /dev/null
+++ b/src/plugins/intel_gpu/src/kernel_selector/kernels/swiglu/swiglu_kernel_base.h
@@ -0,0 +1,38 @@
+// Copyright (C) 2024 Intel Corporation
+// SPDX-License-Identifier: Apache-2.0
+//
+
+#pragma once
+
+#include "kernel_base_opencl.h"
+#include "kernel_selector_params.h"
+#include "intel_gpu/op/swiglu.hpp"
+
+namespace kernel_selector {
+////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+// swiglu_params
+///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+struct swiglu_params : public base_params {
+ swiglu_params() : base_params(KernelType::SWIGLU), axis(0), split_length(0),
+ glu_type(ov::intel_gpu::op::SwiGLU::GluType::Swish), split_to_glu_idx(0) {}
+ int32_t axis;
+ int32_t split_length;
+ ov::intel_gpu::op::SwiGLU::GluType glu_type;
+ int32_t split_to_glu_idx;
+};
+
+class SwiGLUKernelBase : public KernelBaseOpenCL {
+public:
+ using KernelBaseOpenCL::KernelBaseOpenCL;
+ virtual ~SwiGLUKernelBase() {}
+
+ virtual JitConstants GetJitConstants(const swiglu_params& params, const CommonDispatchData& dispatchData) const;
+ virtual CommonDispatchData SetDefault(const swiglu_params& params) const;
+ KernelsData GetKernelsData(const Params& params) const override;
+ Datatype GetAccumulatorType(const swiglu_params& params) const;
+
+protected:
+ bool Validate(const Params&) const override;
+ void GetUpdateDispatchDataFunc(KernelData& kd) const override;
+};
+} // namespace kernel_selector
diff --git a/src/plugins/intel_gpu/src/kernel_selector/kernels/swiglu/swiglu_kernel_opt.cpp b/src/plugins/intel_gpu/src/kernel_selector/kernels/swiglu/swiglu_kernel_opt.cpp
new file mode 100644
index 00000000000..48e3735d2a7
--- /dev/null
+++ b/src/plugins/intel_gpu/src/kernel_selector/kernels/swiglu/swiglu_kernel_opt.cpp
@@ -0,0 +1,29 @@
+// Copyright (C) 2024 Intel Corporation
+// SPDX-License-Identifier: Apache-2.0
+//
+
+#include "swiglu_kernel_opt.h"
+#include "kernel_selector_utils.h"
+#include <string>
+
+namespace kernel_selector {
+ParamsKey SwiGLUKernelOpt::GetSupportedKey() const {
+ ParamsKey k;
+ k.EnableInputDataType(Datatype::F16);
+ k.EnableInputDataType(Datatype::F32);
+ k.EnableOutputDataType(Datatype::F16);
+ k.EnableOutputDataType(Datatype::F32);
+ k.EnableInputLayout(DataLayout::bfyx);
+ k.EnableInputLayout(DataLayout::bfzyx);
+ k.EnableOutputLayout(DataLayout::bfyx);
+ k.EnableOutputLayout(DataLayout::bfzyx);
+ k.EnableBatching();
+ k.EnableDifferentTypes();
+ k.EnableDynamicShapesSupport();
+ return k;
+}
+
+KernelsPriority SwiGLUKernelOpt::GetKernelsPriority(const Params& /*params*/) const {
+ return FORCE_PRIORITY_1;
+}
+} // namespace kernel_selector
diff --git a/src/plugins/intel_gpu/src/kernel_selector/kernels/swiglu/swiglu_kernel_opt.h b/src/plugins/intel_gpu/src/kernel_selector/kernels/swiglu/swiglu_kernel_opt.h
new file mode 100644
index 00000000000..4fe64e6fc91
--- /dev/null
+++ b/src/plugins/intel_gpu/src/kernel_selector/kernels/swiglu/swiglu_kernel_opt.h
@@ -0,0 +1,19 @@
+// Copyright (C) 2024 Intel Corporation
+// SPDX-License-Identifier: Apache-2.0
+//
+
+#pragma once
+
+#include "swiglu_kernel_base.h"
+
+namespace kernel_selector {
+class SwiGLUKernelOpt : public SwiGLUKernelBase {
+public:
+ SwiGLUKernelOpt() : SwiGLUKernelBase("swiglu_gpu_opt") {}
+ virtual ~SwiGLUKernelOpt() {}
+
+protected:
+ KernelsPriority GetKernelsPriority(const Params& params) const override;
+ ParamsKey GetSupportedKey() const override;
+};
+} // namespace kernel_selector
diff --git a/src/plugins/intel_gpu/src/kernel_selector/kernels/swiglu/swiglu_kernel_ref.cpp b/src/plugins/intel_gpu/src/kernel_selector/kernels/swiglu/swiglu_kernel_ref.cpp
index 8a25916f4a2..fc358480fcf 100644
--- a/src/plugins/intel_gpu/src/kernel_selector/kernels/swiglu/swiglu_kernel_ref.cpp
+++ b/src/plugins/intel_gpu/src/kernel_selector/kernels/swiglu/swiglu_kernel_ref.cpp
@@ -25,102 +25,7 @@ ParamsKey SwiGLUKernelRef::GetSupportedKey() const {
return k;
}
-JitConstants SwiGLUKernelRef::GetJitConstants(const swiglu_params& params) const {
- JitConstants jit = MakeBaseParamsJitConstants(params);
-
- jit.AddConstants({MakeJitConstant("AXIS", params.axis)});
- jit.AddConstants({MakeJitConstant("SPLIT_LENGTH", params.split_length)});
- jit.AddConstants({MakeJitConstant("GLU_TYPE", params.glu_type)});
- const std::string type_suffix = (GetAccumulatorType(params) == Datatype::F32) ? "f" : "h";
- if (params.glu_type == ov::intel_gpu::op::SwiGLU::GluType::Gelu) {
- jit.AddConstants({MakeJitConstant("GEGLU_HALF", "0.5" + type_suffix)});
- jit.AddConstants({MakeJitConstant("GEGLU_MULT", "0.7071067811865475" + type_suffix)});
- } else if (params.glu_type == ov::intel_gpu::op::SwiGLU::GluType::Gelu_Tanh) {
- jit.AddConstants({MakeJitConstant("GEGLU_HALF", "0.5" + type_suffix)});
- jit.AddConstants({MakeJitConstant("GEGLU_MULT", "0.044715" + type_suffix)});
- jit.AddConstants({MakeJitConstant("GEGLU_SQUARE_2_OVER_PI", "0.79788458347320556640625" + type_suffix)});
- }
- jit.AddConstants({MakeJitConstant("SPLIT_TO_GLU_IDX", params.split_to_glu_idx)});
- jit.Merge(MakeTypeJitConstants(GetAccumulatorType(params), "ACCUMULATOR"));
- jit.Merge(GetTensorFriendlyWorkGroupsJit(params.outputs[0]));
-
- return jit;
-}
-
-CommonDispatchData SwiGLUKernelRef::SetDefault(const swiglu_params& params) const {
- CommonDispatchData dispatchData;
-
- dispatchData.gws = GetTensorFriendlyWorkGroups(params.outputs[0]);
- dispatchData.lws = GetOptimalLocalWorkGroupSizes(dispatchData.gws, params.engineInfo);
-
- return dispatchData;
-}
-
-void SwiGLUKernelRef::GetUpdateDispatchDataFunc(KernelData& kd) const {
- kd.update_dispatch_data_func = [this](const Params& params, KernelData& kd) {
- const auto& prim_params = static_cast<const swiglu_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 SwiGLUKernelRef::GetKernelsData(const Params& params) const {
- assert(params.GetType() == KernelType::SWIGLU);
-
- if (!Validate(params))
- return {};
-
- const swiglu_params& orgParams = static_cast<const swiglu_params&>(params);
- auto dispatchData = SetDefault(orgParams);
-
- KernelData kd = KernelData::Default<swiglu_params>(params);
-
- auto cldnn_jit = GetJitConstants(orgParams);
- auto entry_point = GetEntryPoint(kernelName, orgParams.layerID, params);
- auto jit = CreateJit(kernelName, cldnn_jit, entry_point);
-
- GetUpdateDispatchDataFunc(kd);
-
- auto& kernel = kd.kernels[0];
- FillCLKernelData(kernel,
- dispatchData,
- params.engineInfo,
- kernelName,
- jit,
- entry_point,
- EXE_MODE_DEFAULT,
- false,
- false,
- 1,
- GetFusedPrimitiveInputsCount(params),
- 1,
- orgParams.is_shape_agnostic);
-
- return {kd};
-}
-
KernelsPriority SwiGLUKernelRef::GetKernelsPriority(const Params& /*params*/) const {
return DONT_USE_IF_HAVE_SOMETHING_ELSE;
}
-
-Datatype SwiGLUKernelRef::GetAccumulatorType(const swiglu_params& params) const {
- Datatype types[] = { Datatype::F32, Datatype::F16, Datatype::INT64, Datatype::INT32, Datatype::UINT32};
-
- for (Datatype type : types)
- for (auto& in : params.inputs)
- if (in.GetDType() == type)
- return type;
-
- return Datatype::F32;
-}
-
-bool SwiGLUKernelRef::Validate(const Params& params) const {
- if (!KernelBaseOpenCL::Validate(params))
- return false;
-
- return true;
-}
} // namespace kernel_selector
diff --git a/src/plugins/intel_gpu/src/kernel_selector/kernels/swiglu/swiglu_kernel_ref.h b/src/plugins/intel_gpu/src/kernel_selector/kernels/swiglu/swiglu_kernel_ref.h
index 61cfc06b13d..ffd4ee2df0e 100644
--- a/src/plugins/intel_gpu/src/kernel_selector/kernels/swiglu/swiglu_kernel_ref.h
+++ b/src/plugins/intel_gpu/src/kernel_selector/kernels/swiglu/swiglu_kernel_ref.h
@@ -4,36 +4,16 @@
#pragma once
-#include "kernel_base_opencl.h"
-#include "intel_gpu/op/swiglu.hpp"
+#include "swiglu_kernel_base.h"
namespace kernel_selector {
-////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
-// swiglu_params
-///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
-struct swiglu_params : public base_params {
- swiglu_params() : base_params(KernelType::SWIGLU), axis(0), split_length(0),
- glu_type(ov::intel_gpu::op::SwiGLU::GluType::Swish), split_to_glu_idx(0) {}
- int32_t axis;
- int32_t split_length;
- ov::intel_gpu::op::SwiGLU::GluType glu_type;
- int32_t split_to_glu_idx;
-};
-
-class SwiGLUKernelRef : public KernelBaseOpenCL {
+class SwiGLUKernelRef : public SwiGLUKernelBase {
public:
- SwiGLUKernelRef() : KernelBaseOpenCL("swiglu_gpu_ref") {}
+ SwiGLUKernelRef() : SwiGLUKernelBase("swiglu_gpu_ref") {}
virtual ~SwiGLUKernelRef() {}
- virtual JitConstants GetJitConstants(const swiglu_params& params) const;
- virtual CommonDispatchData SetDefault(const swiglu_params& params) const;
- KernelsData GetKernelsData(const Params& params) const override;
+protected:
KernelsPriority GetKernelsPriority(const Params& params) const override;
- Datatype GetAccumulatorType(const swiglu_params& params) const;
ParamsKey GetSupportedKey() const override;
-
-protected:
- bool Validate(const Params&) const override;
- void GetUpdateDispatchDataFunc(KernelData& kd) const override;
};
} // namespace kernel_selector
diff --git a/src/plugins/intel_gpu/src/kernel_selector/kernels/swiglu/swiglu_kernel_selector.cpp b/src/plugins/intel_gpu/src/kernel_selector/kernels/swiglu/swiglu_kernel_selector.cpp
index c14389e2102..a2e23843c9a 100644
--- a/src/plugins/intel_gpu/src/kernel_selector/kernels/swiglu/swiglu_kernel_selector.cpp
+++ b/src/plugins/intel_gpu/src/kernel_selector/kernels/swiglu/swiglu_kernel_selector.cpp
@@ -4,9 +4,13 @@
#include "swiglu_kernel_selector.h"
#include "swiglu_kernel_ref.h"
+#include "swiglu_kernel_opt.h"
namespace kernel_selector {
-swiglu_kernel_selector::swiglu_kernel_selector() { Attach<SwiGLUKernelRef>(); }
+swiglu_kernel_selector::swiglu_kernel_selector() {
+ Attach<SwiGLUKernelRef>();
+ Attach<SwiGLUKernelOpt>();
+}
KernelsData swiglu_kernel_selector::GetBestKernels(const Params& params) const {
return GetNaiveBestKernel(params, KernelType::SWIGLU);