-
Notifications
You must be signed in to change notification settings - Fork 44
Expand file tree
/
Copy pathtest_threading.cpp
More file actions
126 lines (105 loc) · 3.75 KB
/
Copy pathtest_threading.cpp
File metadata and controls
126 lines (105 loc) · 3.75 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
119
120
121
122
123
124
125
126
#include <gtest/gtest.h>
#include <thread>
#include <vector>
#include <cmath>
#include <atomic>
extern "C" {
#include "turboquant/turboquant.h"
}
TEST(Threading, ConcurrentQuantize) {
tq_context_t* ctx = nullptr;
tq_status status = tq_init(&ctx, TQ_BACKEND_CPU);
ASSERT_EQ(status, TQ_OK);
std::atomic<int> errors{0};
std::vector<std::thread> threads;
for (int t = 0; t < 4; t++) {
threads.emplace_back([&ctx, &errors, t]() {
float key[128];
for (int i = 0; i < 128; i++) key[i] = sinf(i * 0.1f + t);
block_tq_uniform_4b block;
tq_status s = tq_quantize_keys(ctx, key, 1, 128,
TQ_TYPE_UNIFORM_4B,
&block, sizeof(block));
if (s != TQ_OK) {
errors.fetch_add(1);
}
});
}
for (auto& th : threads) th.join();
EXPECT_EQ(errors.load(), 0);
tq_free(ctx);
}
TEST(Threading, ConcurrentQuantizeMultipleRounds) {
tq_context_t* ctx = nullptr;
tq_status status = tq_init(&ctx, TQ_BACKEND_CPU);
ASSERT_EQ(status, TQ_OK);
std::atomic<int> errors{0};
const int num_threads = 4;
const int rounds = 10;
std::vector<std::thread> threads;
for (int t = 0; t < num_threads; t++) {
threads.emplace_back([&ctx, &errors, t, rounds]() {
for (int r = 0; r < rounds; r++) {
float key[128];
for (int i = 0; i < 128; i++)
key[i] = sinf(i * 0.1f + t * 100 + r);
block_tq_uniform_4b block;
tq_status s = tq_quantize_keys(ctx, key, 1, 128,
TQ_TYPE_UNIFORM_4B,
&block, sizeof(block));
if (s != TQ_OK) {
errors.fetch_add(1);
}
}
});
}
for (auto& th : threads) th.join();
EXPECT_EQ(errors.load(), 0);
tq_free(ctx);
}
TEST(Threading, ConcurrentAttention) {
tq_context_t* ctx = nullptr;
tq_status status = tq_init(&ctx, TQ_BACKEND_CPU);
ASSERT_EQ(status, TQ_OK);
// Use POLAR_3B which has an attention function implemented
const int seq_len = 4;
const int head_dim = 128;
float keys[seq_len * head_dim];
for (int i = 0; i < seq_len * head_dim; i++)
keys[i] = sinf(i * 0.05f);
size_t needed = tq_quantize_keys_size(seq_len, head_dim, TQ_TYPE_POLAR_3B);
std::vector<uint8_t> blocks(needed);
status = tq_quantize_keys(ctx, keys, seq_len, head_dim,
TQ_TYPE_POLAR_3B, blocks.data(), needed);
ASSERT_EQ(status, TQ_OK);
std::atomic<int> errors{0};
std::vector<std::thread> threads;
for (int t = 0; t < 4; t++) {
threads.emplace_back([&ctx, &blocks, &errors, t, seq_len, head_dim]() {
float query[128];
for (int i = 0; i < 128; i++)
query[i] = cosf(i * 0.1f + t);
std::vector<float> scores_vec(seq_len);
float* scores = scores_vec.data();
tq_status s = tq_attention(ctx, query, blocks.data(),
seq_len, head_dim,
TQ_TYPE_POLAR_3B, scores);
if (s != TQ_OK) {
errors.fetch_add(1);
}
});
}
for (auto& th : threads) th.join();
EXPECT_EQ(errors.load(), 0);
tq_free(ctx);
}
TEST(Threading, InitFreeThreadSafe) {
// Verify init/free don't crash when called sequentially
for (int i = 0; i < 10; i++) {
tq_context_t* ctx = nullptr;
tq_status status = tq_init(&ctx, TQ_BACKEND_CPU);
ASSERT_EQ(status, TQ_OK);
ASSERT_NE(ctx, nullptr);
tq_free(ctx);
}
}