// // FmhaV2Execution.hpp // MNN // // Created by MNN on 2024/06/03. // Copyright © 2018, Alibaba Group Holding Limited // #ifdef MNN_SUPPORT_TRANSFORMER_FUSE #ifndef SelfAttentionBufExecution_hpp #define SelfAttentionBufExecution_hpp #include "backend/opencl/execution/image/CommonExecution.hpp" namespace MNN { namespace OpenCL { class SelfAttentionBufImpl { public: SelfAttentionBufImpl(const MNN::Op *op, Backend *backend); ~SelfAttentionBufImpl() = default; ErrorCode onResize(Backend *backend, const std::vector &inputs, const std::vector &outputs); ErrorCode onExecute(Backend *backend, const std::vector &inputs, const std::vector &outputs); private: int getLocalSize(int size, int maxGroupSize); float mScale; int mQseqSplitNum = 1; std::shared_ptr mTempQ, mTempK, mTempQK, mTempSoftMax, mTempV, mTempQKV; std::shared_ptr mTempTrans; int mNumHead = 0, mHeadDim = 0; std::vector> mKernel_split; std::vector> mKernel_qk; std::vector> mKernel_softmax; std::vector> mKernel_qkv; std::vector> mKernel_clip; std::vector> mKernel_trans; std::vector> mGlobalWorkSizeSplit; std::vector> mLocalWorkSizeSplit; std::vector> mGlobalWorkSizeClip; std::vector> mLocalWorkSizeClip; std::vector> mGlobalWorkSizeQk; std::vector> mLocalWorkSizeQk; std::vector> mGlobalWorkSizeSoftMax; std::vector> mLocalWorkSizeSoftMax; std::vector> mGlobalWorkSizeQkv; std::vector> mLocalWorkSizeQkv; std::vector> mGlobalWorkSizeTrans; std::vector> mLocalWorkSizeTrans; uint32_t mMaxWorkGroupSize; OpenCLBackend *mOpenCLBackend; size_t mQkGlobal_size[3]; int mSoftmaxShape[4]; int mByte = 4; cl_recording_qcom mRecording{NULL}; bool mValid = true; }; class SelfAttentionBufExecution : public CommonExecution { public: SelfAttentionBufExecution(const MNN::Op *op, Backend *backend); SelfAttentionBufExecution(std::shared_ptr impl, const MNN::Op *op, Backend *backend); virtual ~SelfAttentionBufExecution() = default; virtual ErrorCode onResize(const std::vector &inputs, const std::vector &outputs) override; virtual ErrorCode onExecute(const std::vector &inputs, const std::vector &outputs) override; virtual bool onClone(Backend* bn, const Op* op, Execution** dst) override; private: std::shared_ptr mImpl; }; } // namespace OpenCL } // namespace MNN #endif /* SelfAttentionBufExecution_hpp */ #endif/* MNN_SUPPORT_TRANSFORMER_FUSE */