MCPcopy Create free account
hub / github.com/cactus-compute/cactus / test_sliding_window_basic

Function test_sliding_window_basic

tests/test_kv_cache.cpp:21–131  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

19}
20
21bool test_sliding_window_basic() {
22 const size_t num_layers = 2;
23 const size_t max_seq = 2048;
24 const size_t num_kv_heads = 8;
25 const size_t head_dim = 64;
26 const size_t window_size = 16;
27 const size_t sink_size = 4;
28
29 KVCache cache;
30 cache.init(num_layers, max_seq, std::vector<size_t>(num_layers, head_dim), std::vector<size_t>(num_layers, num_kv_heads), Precision::INT8);
31 cache.set_window_size(window_size, sink_size);
32
33 CactusGraph graph;
34 graph.input({1}, Precision::FP32);
35
36 {
37 size_t seq_len = 10;
38 vector<size_t> k_nodes, v_nodes;
39
40 for (size_t layer = 0; layer < num_layers; layer++) {
41 size_t k_node = graph.input({seq_len, num_kv_heads, head_dim}, Precision::FP16);
42 size_t v_node = graph.input({seq_len, num_kv_heads, head_dim}, Precision::FP16);
43
44 vector<uint8_t> k_data, v_data;
45 fill_fp16(k_data, seq_len * num_kv_heads * head_dim, layer + 1.0f);
46 fill_fp16(v_data, seq_len * num_kv_heads * head_dim, layer + 2.0f);
47 graph.set_input(k_node, k_data.data(), Precision::FP16);
48 graph.set_input(v_node, v_data.data(), Precision::FP16);
49
50 k_nodes.push_back(k_node);
51 v_nodes.push_back(v_node);
52 }
53
54 graph.execute();
55 cache.update_from_graph(&graph, k_nodes, v_nodes, seq_len, num_layers);
56
57 assert(cache.get_effective_seq_len() == seq_len);
58 }
59
60 {
61 size_t additional_seq = 6;
62 vector<size_t> k_nodes, v_nodes;
63
64 for (size_t layer = 0; layer < num_layers; layer++) {
65 size_t k_node = graph.input({additional_seq, num_kv_heads, head_dim}, Precision::FP16);
66 size_t v_node = graph.input({additional_seq, num_kv_heads, head_dim}, Precision::FP16);
67
68 vector<uint8_t> k_data, v_data;
69 fill_fp16(k_data, additional_seq * num_kv_heads * head_dim, layer + 10.0f);
70 fill_fp16(v_data, additional_seq * num_kv_heads * head_dim, layer + 20.0f);
71 graph.set_input(k_node, k_data.data(), Precision::FP16);
72 graph.set_input(v_node, v_data.data(), Precision::FP16);
73
74 k_nodes.push_back(k_node);
75 v_nodes.push_back(v_node);
76 }
77
78 graph.execute();

Callers 1

mainFunction · 0.85

Calls 12

fill_fp16Function · 0.85
set_window_sizeMethod · 0.80
dataMethod · 0.80
update_from_graphMethod · 0.80
get_effective_seq_lenMethod · 0.80
get_keys_int8Method · 0.80
get_key_scalesMethod · 0.80
get_total_seq_lenMethod · 0.80
initMethod · 0.45
inputMethod · 0.45
set_inputMethod · 0.45
executeMethod · 0.45

Tested by

no test coverage detected