MCPcopy Create free account
hub / github.com/Tencent/TurboTransformers / test_multiple_threads

Function test_multiple_threads

example/cpp/bert_model_test.cpp:122–176  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

120}
121
122static bool test_multiple_threads(bool only_input, int n_threads) {
123 std::shared_ptr<BertModel> model_ptr = std::make_shared<BertModel>(
124 model_file_path, DLDeviceType::kDLCPU, 12, 12);
125 std::vector<std::vector<int64_t>> input_ids{{12166, 10699, 16752, 4454},
126 {5342, 16471, 817, 16022}};
127 std::vector<std::vector<int64_t>> position_ids{{1, 0, 0, 0}, {1, 1, 1, 0}};
128 std::vector<std::vector<int64_t>> segment_ids{{1, 1, 1, 0}, {1, 0, 0, 0}};
129 if (only_input) {
130 position_ids.clear();
131 segment_ids.clear();
132 }
133 std::vector<std::thread> threads;
134 threads.reserve(n_threads);
135
136 std::vector<std::future<std::vector<float>>> result_list;
137 result_list.reserve(n_threads);
138 for (int i = 0; i < n_threads; ++i) {
139 std::packaged_task<std::vector<float>(
140 const std::shared_ptr<BertModel>,
141 const std::vector<std::vector<int64_t>> &,
142 const std::vector<std::vector<int64_t>> &,
143 const std::vector<std::vector<int64_t>> &, PoolType, bool)>
144 task(CallBackFunction);
145 result_list.emplace_back(task.get_future());
146 threads.emplace_back(std::thread(std::move(task), model_ptr, input_ids,
147 position_ids, segment_ids,
148 PoolType::kFirst, true));
149 }
150
151 for (int i = 0; i < n_threads; ++i) {
152 auto vec = result_list[i].get();
153 assert(vec.size() == 768 * 2);
154
155 for (size_t i = 0; i < vec.size(); ++i) {
156 assert(!std::isnan(vec.data()[i]));
157 assert(!std::isinf(vec.data()[i]));
158 }
159 if (only_input) {
160 assert(fabs(vec.data()[0] - 0.9671) < 1e-3);
161 assert(fabs(vec.data()[1] - 0.9860) < 1e-3);
162 assert(fabs(vec.data()[768] - 0.9757) < 1e-3);
163 assert(fabs(vec.data()[768 + 1] - 0.9794) < 1e-3);
164 } else {
165 assert(fabs(vec.data()[0] - 0.9151) < 1e-3);
166 assert(fabs(vec.data()[1] - 0.5919) < 1e-3);
167 assert(fabs(vec.data()[768] - 0.9802) < 1e-3);
168 assert(fabs(vec.data()[768 + 1] - 0.9321) < 1e-3);
169 }
170 }
171
172 for (int i = 0; i < n_threads; ++i) {
173 threads[i].join();
174 }
175 return true;
176}
177TEST_CASE("Bert-multiple-thread", "Cpp interface") {
178 test_multiple_threads(false, 10);
179 test_multiple_threads(true, 10);

Callers 1

Calls 2

dataMethod · 0.80
clearMethod · 0.45

Tested by

no test coverage detected