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"
|
|
|
|
|
|
|
|
namespace MNN {
|
|
|
|
|
|
|
|
|
|
|
|
class CPUAttention : public Execution {
|
|
|
|
public:
|
|
|
|
CPUAttention(Backend *backend, bool kv_cache);
|
|
|
|
virtual ~CPUAttention() = default;
|
|
|
|
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;
|
2024-06-15 15:39:59 +08:00
|
|
|
struct Resource {
|
2024-07-22 19:51:53 +08:00
|
|
|
std::shared_ptr<Tensor> mPastKey; // numhead, [maxlen/eP, headdim, eP]
|
|
|
|
std::shared_ptr<Tensor> mPastValue; // numhead, [headdim/eP, maxlen, eP]
|
|
|
|
std::shared_ptr<Tensor> mDequantKeyScale; // numhead, [maxlen/eP, 1, eP]
|
|
|
|
std::shared_ptr<Tensor> mDequantKeyZeroPoint; // numhead, [maxlen/eP, 1, eP]
|
2024-06-15 15:39:59 +08:00
|
|
|
int mPastLength = 0, mMaxLength = 0;
|
2024-07-22 19:51:53 +08:00
|
|
|
const int mExpandChunk = 64;
|
|
|
|
int mNumHead = 0, mKvNumHead = 0, mHeadDim = 0;
|
2024-06-15 15:39:59 +08:00
|
|
|
};
|
2024-05-11 19:17:02 +08:00
|
|
|
private:
|
2024-07-22 19:51:53 +08:00
|
|
|
void allocKVCache(int kv_seq_len, bool quantK, bool quantV);
|
|
|
|
void reallocKVCache(int kv_seq_len, bool quantK, bool quantV);
|
|
|
|
bool mIsPrefill = true;
|
|
|
|
bool mIsFirstPrefill = true;
|
2024-06-15 15:39:59 +08:00
|
|
|
bool mKVCache;
|
|
|
|
int mThreadNum = 1;
|
|
|
|
std::shared_ptr<Resource> mResource;
|
2024-07-04 11:53:45 +08:00
|
|
|
std::shared_ptr<Tensor> mPackQ, mPackQKV;
|
|
|
|
int eP, lP, hP, bytes, unit;
|
2024-05-11 19:17:02 +08:00
|
|
|
};
|
|
|
|
} // namespace MNN
|
|
|
|
|
|
|
|
#endif // CPUATTENTION_HPP
|
|
|
|
#endif
|