-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbatched_threading.cpp
191 lines (152 loc) · 5.37 KB
/
batched_threading.cpp
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
// PyTorch
#include <torch/torch.h>
#include <torch/types.h>
#include <torch/script.h>
// C++ STD
#include <chrono>
#include <cmath>
#include <iostream>
#include <list>
#include <map>
#include <string>
#include <thread>
using namespace std;
int WARM_UP = 100;
int TOTAL_BATCHES = 10000 + WARM_UP;
typedef unordered_map<string, c10::IValue> KWARG;
struct BatchQueue {
BatchQueue() {
}
~BatchQueue() {
}
void enqueue(vector<KWARG> batch) {
{
lock_guard<mutex> lock(m_);
batches_.push(std::move(batch));
}
cv_.notify_one();
}
vector<KWARG> dequeue()
{
{
unique_lock<mutex> lock(m_);
if(batches_.size()==0)
return vector<KWARG>();
cv_.wait(lock, [&]{return batches_.size();});
}
vector<KWARG> batch;
bool terminate = true;
{
lock_guard<mutex> lock(m_);
if(batches_.size() ) {
batch = std::move(batches_.front());
batches_.pop();
if(batches_.size() % 100 == 0)
cout << "Queue size: " << batches_.size() << endl;
terminate = batches_.size() == 0;
}
}
if(terminate)
cv_.notify_all();
return batch;
}
condition_variable cv_;
mutex m_;
queue<vector<KWARG>> batches_;
};
class TorchScriptWorker{
public:
TorchScriptWorker(shared_ptr<BatchQueue> queue)
:queue_(queue){
wait_waiting = 0;
wait_counter = 0;
for(int i=0; i<4;++i)
threads_.emplace_back([this, i]{process(i);});
}
~TorchScriptWorker(){
for(auto &t : threads_)
t.join();
}
void process(int idx){
auto model = torch::jit::load("../models/bert_model_only_traced.pt");
model.to(at::Device(torch::kCUDA, idx));
for(int i=0; i<WARM_UP; ++i) {
vector<KWARG> batch = queue_->dequeue();
if(batch.size() == 0)
break;
do_work_on_batch(model, move(batch), idx);
}
cout << "Finished warmup: " << idx << endl;
wait();
cout << "Starting for real: " << idx << endl;
while(true) {
vector<KWARG> batch = queue_->dequeue();
if(batch.size() == 0)
break;
do_work_on_batch(model, move(batch), idx);
}
}
void do_work_on_batch(torch::jit::script::Module& model, vector<KWARG> batch, int idx){
vector<torch::Tensor> input_ids, token_type_ids, attention_mask;
for(auto &kw : batch) {
input_ids.push_back(kw["input_ids"].toTensor());
token_type_ids.push_back(kw["token_type_ids"].toTensor());
attention_mask.push_back(kw["attention_mask"].toTensor());
}
KWARG input_data;
input_data["input_ids"] = torch::stack(input_ids).pin_memory().to(at::Device(torch::kCUDA, idx));
input_data["token_type_ids"] = torch::stack(token_type_ids).pin_memory().to(at::Device(torch::kCUDA, idx));
input_data["attention_mask"] = torch::stack(attention_mask).pin_memory().to(at::Device(torch::kCUDA, idx));
chrono::steady_clock::time_point begin = chrono::steady_clock::now();
auto ret = model.forward({}, input_data).toIValue().toTuple()->elements()[0];
auto res = torch::softmax(ret.toTensor(),1);
chrono::steady_clock::time_point end = chrono::steady_clock::now();
{
lock_guard<mutex> lock(m_);
if(chrono::duration_cast<chrono::milliseconds>(end - begin).count()>50)
cout << "Processing time (ms): " << chrono::duration_cast<chrono::milliseconds>(end - begin).count() << endl;
}
vector<string> answers;
for(int i=0; i<batch.size(); ++i) {
float paraphrased_percent = 100.0 * res[i][1].item<float>();
answers.push_back(to_string((int)round(paraphrased_percent)) + "% paraphrase");
}
}
void wait() {
std::unique_lock<std::mutex> lk(wait_m_);
++wait_counter;
++wait_waiting;
wait_cv_.wait(lk, [&]{return wait_counter >= threads_.size();});
wait_cv_.notify_one();
--wait_waiting;
if(wait_waiting == 0)
wait_counter = 0;
lk.unlock();
}
mutex m_;
shared_ptr<BatchQueue> queue_;
vector<thread> threads_;
mutex wait_m_;
condition_variable wait_cv_;
int wait_counter;
int wait_waiting;
};
int main(const int argc, const char* const argv[]) {
shared_ptr<BatchQueue> batch_queue = make_shared<BatchQueue>();
for(int i=0; i< TOTAL_BATCHES; ++i) {
vector<KWARG> batch;
for(int j=0; j<8; ++j)
{
KWARG kwargs;
kwargs["input_ids"] = torch::tensor(std::vector<int64_t>{
101, 1109, 1419, 20164, 10932, 2271, 7954, 1110, 1359, 1107,
1203, 1365, 1392, 102, 7302, 1116, 1132, 2108, 2213, 1111,
1240, 2332, 102});
kwargs["token_type_ids"] = torch::tensor(std::vector<int64_t>{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1});
kwargs["attention_mask"] = torch::tensor(std::vector<int64_t>{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1});
batch.push_back(kwargs);
}
batch_queue->enqueue(batch);
}
TorchScriptWorker worker(batch_queue);
}