LlamaCacheManager.cc 5.3 KB
Newer Older
Li Zhang's avatar
Li Zhang committed
1
2
// Copyright (c) OpenMMLab. All rights reserved.

lvhan028's avatar
lvhan028 committed
3
4
5
#include "src/turbomind/models/llama/LlamaCacheManager.h"
#include "src/turbomind/utils/cuda_utils.h"
#include "src/turbomind/utils/logger.h"
Li Zhang's avatar
Li Zhang committed
6

lvhan028's avatar
lvhan028 committed
7
namespace turbomind {
Li Zhang's avatar
Li Zhang committed
8
9
10
11
12
13
14
15
16
17
18

LlamaCacheManager::~LlamaCacheManager()
{
    for (auto& p : device_mem_) {
        allocator_->free(&p, false);
    }
}

void* LlamaCacheManager::allocate(bool is_preallocte)
{
    if (rank_ == 0) {
lvhan028's avatar
lvhan028 committed
19
        TM_LOG_INFO("[LlamaCacheManager][allocate]");
Li Zhang's avatar
Li Zhang committed
20
21
22
23
24
25
26
27
28
    }

    void* mem_ptr{};

    if (!device_free_.empty()) {
        mem_ptr = device_free_.front();
        device_free_.pop();

        if (rank_ == 0) {
lvhan028's avatar
lvhan028 committed
29
            TM_LOG_INFO("[LlamaCacheManager][allocate] free = %d", (int)device_free_.size());
Li Zhang's avatar
Li Zhang committed
30
31
32
33
34
35
36
        }
    }
    else if (entry_count_ < max_entry_count_) {
        const auto   alloc_count     = std::min(chunk_size_, max_entry_count_ - entry_count_);
        const size_t entry_byte_size = 2 * cache_byte_size_;  // 2 for k,v

        if (rank_ == 0) {
lvhan028's avatar
lvhan028 committed
37
            TM_LOG_INFO("[LlamaCacheManager][allocate] malloc %d", (int)alloc_count);
Li Zhang's avatar
Li Zhang committed
38
39
40
41
42
43
        }
        const auto chunk_ptr = allocator_->malloc(alloc_count * entry_byte_size, false);
        FT_CHECK(chunk_ptr);
        device_mem_.push_back(chunk_ptr);
        entry_count_ += alloc_count;
        if (rank_ == 0) {
lvhan028's avatar
lvhan028 committed
44
            TM_LOG_INFO("[LlamaCacheManager][allocate] count = %d", entry_count_);
Li Zhang's avatar
Li Zhang committed
45
46
47
48
49
50
51
52
53
54
55
56
        }

        for (int i = 0; i < alloc_count; ++i) {
            device_free_.push((uint8_t*)chunk_ptr + entry_byte_size * i);
        }

        if (!is_preallocte) {
            mem_ptr = device_free_.front();
            device_free_.pop();
        }

        if (rank_ == 0) {
lvhan028's avatar
lvhan028 committed
57
            TM_LOG_INFO("[LlamaCacheManager][allocate] free = %d", (int)device_free_.size());
Li Zhang's avatar
Li Zhang committed
58
59
60
61
62
63
64
65
66
67
68
69
70
        }
    }
    else {
        mem_ptr = evict();
        FT_CHECK_WITH_INFO(mem_ptr, "No enough cache entries.");
    }

    return mem_ptr;
}

auto LlamaCacheManager::create(uint64_t id, cudaStream_t stream) -> Sequence
{
    if (rank_ == 0) {
lvhan028's avatar
lvhan028 committed
71
        TM_LOG_INFO("[LlamaCacheManager][create] %ld", (long)id);
Li Zhang's avatar
Li Zhang committed
72
73
74
75
76
    }

    for (const auto& e : device_cache_) {
        if (e.id == id) {
            if (rank_ == 0) {
lvhan028's avatar
lvhan028 committed
77
                TM_LOG_WARNING("[LlamaCacheManager][create] Removing conflicting id %ld", (long)id);
Li Zhang's avatar
Li Zhang committed
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
            }
            erase(id);
        }
    }

    const auto mem_ptr = (uint8_t*)allocate(false);
    check_cuda_error(cudaMemsetAsync(mem_ptr, 0, cache_byte_size_ * 2, stream));

    device_cache_.push_back({
        id,
        max_seq_len_,
        {},
        0,
        mem_ptr,
        mem_ptr + cache_byte_size_,
        {},
        static_cast<uint64_t>(-1),
    });

    return device_cache_.back();
}

auto LlamaCacheManager::getEntryOrThrow(uint64_t id) -> std::vector<Sequence>::iterator
{
    auto pred = [&](const Sequence& s) { return s.id == id; };
    auto it   = std::find_if(device_cache_.begin(), device_cache_.end(), pred);
    if (it == device_cache_.end()) {
lvhan028's avatar
lvhan028 committed
105
        TM_LOG_ERROR("[LlamaCacheManager] %ld not found.\n", (long)id);
Li Zhang's avatar
Li Zhang committed
106
107
108
109
110
111
112
113
        FT_CHECK(0);
    }
    return it;
}

auto LlamaCacheManager::fetch(uint64_t id, cudaStream_t stream) -> Sequence
{
    if (rank_ == 0) {
lvhan028's avatar
lvhan028 committed
114
        TM_LOG_INFO("[LlamaCacheManager][fetch] %ld", (long)id);
Li Zhang's avatar
Li Zhang committed
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
    }

    auto entry = getEntryOrThrow(id);

    if (entry->k_cache == nullptr) {
        FT_CHECK(entry->cache_len == 0);
        const auto mem_ptr = allocate(false);
        check_cuda_error(cudaMemsetAsync(mem_ptr, 0, cache_byte_size_ * 2, stream));
        entry->k_cache = mem_ptr;
        entry->v_cache = (uint8_t*)entry->k_cache + cache_byte_size_;
    }

    entry->timestamp = static_cast<uint64_t>(-1);
    return *entry;
}

void LlamaCacheManager::update(const Sequence& seq, cudaStream_t stream)
{
    if (rank_ == 0) {
lvhan028's avatar
lvhan028 committed
134
        TM_LOG_INFO("[LlamaCacheManager][update] %ld", (long)seq.id);
Li Zhang's avatar
Li Zhang committed
135
136
137
138
139
140
141
142
143
144
145
146
147
    }

    auto entry = getEntryOrThrow(seq.id);

    entry->timestamp = ++timestamp_;
    entry->token_ids = seq.token_ids;
    entry->cache_len = seq.cache_len;
    FT_CHECK(seq.k_cache == entry->k_cache && seq.v_cache == entry->v_cache);
}

void LlamaCacheManager::erase(uint64_t id)
{
    if (rank_ == 0) {
lvhan028's avatar
lvhan028 committed
148
        TM_LOG_INFO("[LlamaCacheManager][erase] %ld", (long)id);
Li Zhang's avatar
Li Zhang committed
149
150
151
152
153
154
155
    }

    auto entry = getEntryOrThrow(id);

    if (entry->k_cache) {
        device_free_.push(entry->k_cache);
        if (rank_ == 0) {
lvhan028's avatar
lvhan028 committed
156
            TM_LOG_INFO("[LlamaCacheManager][erase] free = %d", (int)device_free_.size());
Li Zhang's avatar
Li Zhang committed
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
        }
    }
    device_cache_.erase(entry);
}

void* LlamaCacheManager::evict()
{
    FT_CHECK(!device_cache_.empty());
    auto it = std::min_element(device_cache_.begin(), device_cache_.end(), [](const auto& a, const auto& b) {
        return a.timestamp < b.timestamp;
    });

    if (it->timestamp == static_cast<uint64_t>(-1)) {
        return nullptr;
    }

    if (rank_ == 0) {
lvhan028's avatar
lvhan028 committed
174
        TM_LOG_INFO("[LlamaCacheManager][evict] %ld", (long)it->id);
Li Zhang's avatar
Li Zhang committed
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
    }

    FT_CHECK(it->k_cache);
    auto mem_ptr = it->k_cache;
    it->k_cache = it->v_cache = nullptr;
    it->cache_len             = 0;
    it->timestamp             = static_cast<uint64_t>(-1);
    return mem_ptr;
}

bool LlamaCacheManager::contains(uint64_t id) const noexcept
{
    auto pred = [&](const Sequence& s) { return s.id == id; };
    auto it   = std::find_if(device_cache_.begin(), device_cache_.end(), pred);
    return it != device_cache_.end();
}

lvhan028's avatar
lvhan028 committed
192
}  // namespace turbomind