Repository navigation
Expand file tree
/
Copy pathcached_decode.h
More file actions
118 lines (99 loc) · 3.48 KB
/
Copy pathcached_decode.h
File metadata and controls
118 lines (99 loc) · 3.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
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
84
85
86
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
118
#pragma once
#include "engine/framework/runtime/kv_cache.h"
#include <cstdint>
#include <functional>
#include <memory>
#include <stdexcept>
#include <utility>
namespace engine::runtime {
struct CachedDecodeStep {
int64_t position = 0;
int64_t valid_steps = 0;
int64_t cache_steps = 0;
int32_t cache_slot = 0;
int64_t logical_start = 0;
bool wrapped = false;
};
template <typename Graph, typename Policy>
class CachedDecodeRuntime {
public:
using Factory = std::function<std::unique_ptr<Graph>(int64_t cache_steps)>;
using Config = typename Policy::Config;
CachedDecodeRuntime() = default;
explicit CachedDecodeRuntime(Config config)
: policy_(std::move(config)) {}
bool prepare_for_prefill(int64_t required_steps, const Factory & factory) {
const int64_t target_steps = target_cache_steps(required_steps);
if (graph_ != nullptr && graph_->cache_steps() == target_steps) {
return false;
}
graph_ = factory(target_steps);
policy_.reset();
return true;
}
bool grow_for_next_step(int64_t required_steps, const Factory & factory) {
const int64_t target_steps = target_cache_steps(required_steps);
if (graph_ != nullptr && graph_->cache_steps() >= target_steps) {
return false;
}
if (graph_ == nullptr) {
throw std::runtime_error(policy_.label() + " requires an initialized graph before cache growth");
}
auto state = graph_->export_state();
graph_ = factory(target_steps);
import_state(state);
return true;
}
void import_state(const TransformerKVState & state) {
if (graph_ == nullptr) {
throw std::runtime_error(policy_.label() + " requires a graph before importing KV state");
}
graph_->import_state(state);
policy_.import_state(state, graph_->cache_steps());
}
void reset_to_empty_cache() {
if (graph_ == nullptr) {
throw std::runtime_error(policy_.label() + " requires a graph before reset");
}
graph_->reset_state();
policy_.reset_to_empty(graph_->cache_steps());
}
CachedDecodeStep next_step() const {
if (graph_ == nullptr) {
throw std::runtime_error(policy_.label() + " requires an initialized graph before decode");
}
return policy_.next_step();
}
void advance_after_direct_append(int64_t steps) {
if (graph_ == nullptr) {
throw std::runtime_error(policy_.label() + " requires an initialized graph before cache advance");
}
const auto before = policy_.next_step();
if (policy_.should_advance_graph_cache(before, steps)) {
graph_->advance_cache_after_direct_append(steps);
}
policy_.advance_after_direct_append(steps);
}
bool has_graph() const noexcept {
return graph_ != nullptr;
}
Graph & graph() {
if (graph_ == nullptr) {
throw std::runtime_error(policy_.label() + " graph is not initialized");
}
return *graph_;
}
const Graph & graph() const {
if (graph_ == nullptr) {
throw std::runtime_error(policy_.label() + " graph is not initialized");
}
return *graph_;
}
int64_t target_cache_steps(int64_t required_steps) const {
return policy_.target_cache_steps(required_steps);
}
private:
Policy policy_;
std::unique_ptr<Graph> graph_;
};
} // namespace engine::runtime