opcache_manager.hpp 5.63 KB
Newer Older
wooway777's avatar
wooway777 committed
1
2
3
4
5
6
7
8
9
10
11
12
#ifndef CACHE_MANAGER_HPP
#define CACHE_MANAGER_HPP

#include <functional>
#include <memory>
#include <unordered_map>
#include <vector>

#include "../tensor.hpp"
#include "../utils.hpp"
#include "infinicore_infer.h"

13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
class IDescriptorDestroyer {
public:
    virtual ~IDescriptorDestroyer() = default;
    virtual void destroy(void *descriptor) = 0;
};

template <typename DescriptorType>
class DescriptorDestroyer : public IDescriptorDestroyer {
    using DestroyFunc = infiniStatus_t (*)(DescriptorType);
    DestroyFunc destroyFunc;

public:
    DescriptorDestroyer(DestroyFunc func) : destroyFunc(func) {}

    void destroy(void *descriptor) override {
        destroyFunc(*static_cast<DescriptorType *>(descriptor));
    }
wooway777's avatar
wooway777 committed
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
};

template <typename DescriptorType>
class LRUDescriptorCache {
private:
    struct CacheNode {
        size_t key;
        DescriptorType desc;
        CacheNode *prev;
        CacheNode *next;

        CacheNode() : key(0), desc(), prev(nullptr), next(nullptr) {}
        CacheNode(size_t k, const DescriptorType &d) : key(k), desc(d), prev(nullptr), next(nullptr) {}
    };

    std::unordered_map<size_t, CacheNode *> cache;
    CacheNode *head;
    CacheNode *tail;
    const size_t capacity;
    size_t size;
50
    std::unique_ptr<IDescriptorDestroyer> destroyer;
wooway777's avatar
wooway777 committed
51
52
53
54

    void removeNode(CacheNode *node) {
        node->prev->next = node->next;
        node->next->prev = node->prev;
55
56
57
        if (destroyer) {
            destroyer->destroy(&node->desc);
        }
wooway777's avatar
wooway777 committed
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
        cache.erase(node->key);
        delete node;
        --size;
    }

    void addToTop(CacheNode *node) {
        node->next = head->next;
        node->next->prev = node;
        node->prev = head;
        head->next = node;
        cache[node->key] = node;
        if (++size > capacity) {
            removeNode(tail->prev);
        }
    }

    void moveToTop(CacheNode *node) {
        node->prev->next = node->next;
        node->next->prev = node->prev;
        node->next = head->next;
        node->next->prev = node;
        node->prev = head;
        head->next = node;
    }

public:
84
85
86
    template <typename DestroyFunc>
    LRUDescriptorCache(size_t c, DestroyFunc destroyFunc)
        : capacity(c), size(0), destroyer(std::make_unique<DescriptorDestroyer<DescriptorType>>(destroyFunc)) {
wooway777's avatar
wooway777 committed
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
        head = new CacheNode();
        tail = new CacheNode();
        head->next = tail;
        tail->prev = head;
    }

    ~LRUDescriptorCache() {
        while (head->next != tail) {
            removeNode(head->next);
        }
        delete head;
        delete tail;
    }

    bool get(size_t key, DescriptorType &out_desc) {
        auto it = cache.find(key);
        if (it == cache.end()) {
            return false;
        }

        CacheNode *node = it->second;
        moveToTop(node);
        out_desc = node->desc;
        return true;
    }

    void put(size_t key, const DescriptorType &descriptor) {
        auto it = cache.find(key);
        if (it != cache.end()) {
            // Key already exists, update the descriptor
            CacheNode *node = it->second;
118
119
120
            if (destroyer) {
                destroyer->destroy(&node->desc);
            }
wooway777's avatar
wooway777 committed
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
            node->desc = descriptor;
            moveToTop(node);
            return;
        }

        // Check if we need to evict
        if (size >= capacity) {
            removeNode(tail->prev);
        }

        // Create new node and add to top
        CacheNode *node = new CacheNode(key, descriptor);
        addToTop(node);
    }

    LRUDescriptorCache(const LRUDescriptorCache &) = delete;
    LRUDescriptorCache &operator=(const LRUDescriptorCache &) = delete;
};

wooway777's avatar
wooway777 committed
140
141
// Helper macro to generate the destroy function name
#define DESTROY_FUNC(OpType) infiniopDestroy##OpType##Descriptor
blkmjsian's avatar
blkmjsian committed
142

wooway777's avatar
wooway777 committed
143
144
145
146
147
148
149
150
// Declare cache and access functions
#define DECLARE_OP_CACHE(OpType)                                                           \
    LRUDescriptorCache<infiniop##OpType##Descriptor_t> OpType##_cache;                     \
    bool get##OpType##Descriptor(size_t key, infiniop##OpType##Descriptor_t &desc) {       \
        return OpType##_cache.get(key, desc);                                              \
    }                                                                                      \
    void put##OpType##Descriptor(size_t key, const infiniop##OpType##Descriptor_t &desc) { \
        OpType##_cache.put(key, desc);                                                     \
wooway777's avatar
wooway777 committed
151
152
    }

wooway777's avatar
wooway777 committed
153
154
155
156
157
158
159
160
161
162
163
class CacheManager {
public:
    DECLARE_OP_CACHE(Add)
    DECLARE_OP_CACHE(RMSNorm)
    DECLARE_OP_CACHE(Gemm)
    DECLARE_OP_CACHE(RoPE)
    DECLARE_OP_CACHE(Rearrange)
    DECLARE_OP_CACHE(CausalSoftmax)
    DECLARE_OP_CACHE(Topkrouter)
    DECLARE_OP_CACHE(SwiGLU)
    DECLARE_OP_CACHE(RandomSample)
164
    DECLARE_OP_CACHE(DequantizeAWQ)
blkmjsian's avatar
blkmjsian committed
165

wooway777's avatar
wooway777 committed
166
167
168
169
170
171
172
173
174
175
    CacheManager(size_t capacity = 100)
        : Add_cache(capacity, DESTROY_FUNC(Add)),
          RMSNorm_cache(capacity, DESTROY_FUNC(RMSNorm)),
          Gemm_cache(capacity, DESTROY_FUNC(Gemm)),
          RoPE_cache(capacity, DESTROY_FUNC(RoPE)),
          Rearrange_cache(capacity, DESTROY_FUNC(Rearrange)),
          CausalSoftmax_cache(capacity, DESTROY_FUNC(CausalSoftmax)),
          Topkrouter_cache(capacity, DESTROY_FUNC(Topkrouter)),
          SwiGLU_cache(capacity, DESTROY_FUNC(SwiGLU)),
          RandomSample_cache(capacity, DESTROY_FUNC(RandomSample)),
176
          DequantizeAWQ_cache(capacity, DESTROY_FUNC(DequantizeAWQ)) {}
blkmjsian's avatar
blkmjsian committed
177

178
179
    template <typename... Tensors>
    static size_t createDescriptorKey(Tensors... tensors) {
wooway777's avatar
wooway777 committed
180
        size_t seed = 0;
181
        (..., (tensors ? hash_combine(seed, tensors->seed()) : (void)0));
wooway777's avatar
wooway777 committed
182
183
184
185
        return seed;
    }
};

wooway777's avatar
wooway777 committed
186
187
188
#undef DESTROY_FUNC
#undef DECLARE_OP_CACHE

wooway777's avatar
wooway777 committed
189
#endif // CACHE_MANAGER_HPP