MNN/source/backend/cpu/CPUAttention.hpp

50 lines
1.6 KiB
C++
Raw Normal View History

2024-05-11 19:17:02 +08:00
//
// CPUAttention.hpp
// MNN
//
// Created by MNN on 2024/03/19.
// Copyright © 2018, Alibaba Group Holding Limited
//
#ifdef MNN_SUPPORT_TRANSFORMER_FUSE
#ifndef CPUATTENTION_HPP
#define CPUATTENTION_HPP
#include <functional>
#include "core/Execution.hpp"
2024-12-31 15:34:08 +08:00
#include "core/OpCommonUtils.hpp"
2024-08-24 15:46:21 +08:00
#include "MNN/ErrorCode.hpp"
#include "KVCacheManager.hpp"
2024-05-11 19:17:02 +08:00
namespace MNN {
class CPUAttention : public Execution {
public:
CPUAttention(Backend *backend, bool kv_cache);
2024-08-24 15:46:21 +08:00
virtual ~CPUAttention();
2024-05-11 19:17:02 +08:00
virtual ErrorCode onResize(const std::vector<Tensor *> &inputs, const std::vector<Tensor *> &outputs) override;
virtual ErrorCode onExecute(const std::vector<Tensor *> &inputs, const std::vector<Tensor *> &outputs) override;
virtual bool onClone(Backend* bn, const Op* op, Execution** dst) override;
private:
2024-08-24 15:46:21 +08:00
bool mKVCache = true;
2024-09-12 12:57:57 +08:00
bool mUseGemmInt8 = false;
2024-08-24 15:46:21 +08:00
int bytes = 4;
int mThreadNum = 1;;
2024-09-12 12:57:57 +08:00
int eP, lP, hP, unit; // float matmul packing
int eP8, lP8, hP8; // GemmInt8 packing
2024-08-24 15:46:21 +08:00
int mNumHead, mKvNumHead, mHeadDim;
2024-09-12 12:57:57 +08:00
std::shared_ptr<Tensor> mPackQ, mPackQKV, mSumQ;
2024-08-24 15:46:21 +08:00
std::shared_ptr<KVCacheManager> mKVCacheManager = nullptr;
2024-09-12 12:57:57 +08:00
std::vector<float> mMinQ, mMaxQ, mQueryScale, mQueryZeroPoint;
template <typename T> void pack_query(Tensor* query, char* pack_q, char* sum_q, int seq_len, int h, float q_scale);
template <typename T> void unpack_QK(float * unpack_qk_dst, char * pack_qk_src, int seq_len, int kv_seq_len);
2024-12-31 15:34:08 +08:00
KVMeta* mMeta;
2024-05-11 19:17:02 +08:00
};
2024-08-24 15:46:21 +08:00
2024-05-11 19:17:02 +08:00
} // namespace MNN
#endif // CPUATTENTION_HPP
2024-08-24 15:46:21 +08:00
#endif // MNN_SUPPORT_TRANSFORMER_FUSE