LIMA
Libre Multilingual Analyzer — C++ API
Loading...
Searching...
No Matches
segmentation_impl.cpp
Go to the documentation of this file.
1// Copyright 2002-2023 CEA LIST
2// SPDX-FileCopyrightText: 2023 CEA LIST <gael.de-chalendar@cea.fr>
3//
4// SPDX-License-Identifier: MIT
5
7
9
19
20// SegmentationImpl::SegmentationImpl(
21// const std::vector<ngram_descr_t>& ngram_descr,
22// size_t threads,
23// size_t buffer_size_per_thread
24// )
25// : SegmentationClassifier(
26// ngram_descr.size() * 2, 4, threads * 2, buffer_size_per_thread, threads),
27// m_input_encoder(ngram_descr),
28// m_decoder(SegmentationClassifier::get_output(), m_char_len),
29// m_current_timepoint(SegmentationClassifier::get_start_timepoint()),
30// m_buff_set(SegmentationClassifier::get_num_threads() * 2, SegmentationClassifier::get_slot_size() * 4)
31// {
32// m_char_len.resize(SegmentationClassifier::size());
33// }
34
35void SegmentationImpl::load(const std::string& fn)
36{
37 try
38 {
40 }
41 catch (std::runtime_error& e)
42 {
43 std::cerr << "SegmentationImpl exception while trying to load model " << fn << ":" << std::endl
44 << e.what();
45 throw;
46 }
47 m_input_encoder.init(SegmentationClassifier::get_ngram_descr());
48 // The lexicon feature's word list, copied beside the model at training
49 m_lexicon.reset();
50 for (const auto& nd : SegmentationClassifier::get_ngram_descr())
51 {
52 if (ngram_descr_t::lexicon_ngram == nd.m_type)
53 {
54 m_lexicon = std::make_shared<SegmLexicon>(fn + ".lex");
55 std::cerr << "Loaded segmenter lexicon (" << m_lexicon->size() << " words) from "
56 << fn << ".lex" << std::endl;
57 break;
58 }
59 }
60 m_input_encoder.set_lexicon(m_lexicon.get());
61}
62
63void SegmentationImpl::init(size_t threads, size_t buffer_size_per_thread)
64{
65 SegmentationClassifier::set_dicts(SegmentationClassifier::get_dicts()); // This line needs refactoring.
66 SegmentationClassifier::init(SegmentationClassifier::get_ngram_descr().size(),
67 4, threads * 2 + 2, buffer_size_per_thread, threads);
68 m_char_len.resize(SegmentationClassifier::size());
72}
73
75{
76 // std::cerr << "SegmentationImpl::parse_from_stream" << std::endl;
77 size_t n = 0;
78 bool just_started = true;
79 bool continue_reading = true;
80 uint64_t counter = 0;
81 reset(); // reset internal slots for (re)use
82 // Everything else a previous text changed, so that one instance analyzes
83 // several texts (it stalled on the second one, aymara/lima#172: the slot
84 // indexes of the previous text were kept)
85 m_input_encoder.reset();
86 m_decoder.reset();
94
95 do
96 {
97 locked_buffer_t& buff = m_buff_set.get(n);
98
99 while (buff.locked())
100 {
102 }
103
104 int32_t bytes_read = 0;
105 continue_reading = fn(buff.m_data, bytes_read, m_buff_set.max_buff_size());
106 if (0 == bytes_read)
107 {
108 break;
109 }
110 counter += bytes_read;
111 // std::cerr << "SegmentationImpl::parse_from_stream Reading callback: "
112 // << bytes_read << " bytes, continue_reading="
113 // << continue_reading << " counter=" << counter << std::endl;
114 buff.m_char_aligned_data = (const char*)(buff.m_data);
115 buff.m_len = bytes_read;
116 // std::cerr << "SegmentationImpl::parse_from_stream locking (m_buff_set) buff "
117 // << n << std::endl;
118 buff.lock();
119
120 int32_t pos = 0;
121 uint8_t* p = buff.m_data;
122 if (!just_started && 0 == n)
123 {
124 memcpy(p - 8,
125 m_buff_set.get(m_buff_set.size() - 1).m_data
126 + m_buff_set.max_buff_size() - 8,
127 8);
128 }
129
130 // Warming up is required in the beginning of the text
131 while (! m_input_encoder.ready_to_generate())
132 {
133 m_input_encoder.warmup(p, &pos, bytes_read);
134 }
135
136 uint32_t lookbehind = 0;
137 if (0 == pos)
138 {
139 // parse_start can handle bytes remaining from the previous buffer
140 if ((lookbehind = m_input_encoder.parse_start(p, &pos, bytes_read)) > 0)
141 {
142 assert(lookbehind <= 6);
144 }
145 }
146
147 while (pos < bytes_read)
148 {
149 if (m_input_encoder.parse(p, &pos, bytes_read) > 0)
150 {
152 }
153 }
154
155 n = m_buff_set.next(n);
156 just_started = false;
157 } while (continue_reading);
158
159 char final_spaces[] = " ";
160 for (size_t i = 0; i < m_input_encoder.get_lookahead(); i++)
161 {
162 int32_t pos = 0;
163 if (m_input_encoder.parse((uint8_t*)final_spaces, &pos, 1) > 0)
164 {
166 }
167 else
168 {
169 throw std::runtime_error("Something wrong.");
170 }
171 }
172
173 no_more_data();
174
175 for (size_t i = 0; i < m_buff_set.size(); i++)
176 {
177 locked_buffer_t& buff = m_buff_set.get(n);
178
179 while (buff.locked())
180 {
182 }
183
184 // m_buff_set.pretty_print();
185 // SegmentationClassifier::pretty_print();
186
187 n = m_buff_set.next(n);
188 }
189
190 // The last token, if the model left it open
191 m_decoder.flush();
192}
193
195{
196 m_decoder.register_handler(fn);
197}
198
200{
201 for (size_t i = 0; i < m_input_encoder.size(); i++)
202 {
203 SegmentationClassifier::set(timepoint, i, m_input_encoder.get_feat(i));
204 }
205
207}
208
215
216void SegmentationImpl::send_results(int32_t slot_idx)
217{
218 uint64_t from = SegmentationClassifier::get_slot_begin(slot_idx);
219 const uint64_t to = SegmentationClassifier::get_slot_end(slot_idx);
220
221 while (from < to)
222 {
223 const char* pch = m_buff_set.get(m_curr_buff_idx).m_char_aligned_data;
224 uint32_t max = m_buff_set.bytes_available(m_curr_buff_idx);
225
226 if (0 == max || nullptr == pch)
227 {
228 break;
229 }
230
231 from = m_decoder.decode(&pch, max, from, to);
232 assert(nullptr != pch);
233
234 for (size_t i = m_curr_buff_idx; i < m_buff_set.size(); i++)
235 {
236 uint8_t* buff_end = m_buff_set.get(i).end();
237 if (nullptr == buff_end)
238 {
239 break;
240 }
241 if ((uint8_t*)pch >= buff_end)
242 {
243 m_buff_set.get(i).unlock();
244 m_curr_buff_idx = m_buff_set.next(i);
245 }
246 else
247 {
248 m_buff_set.get(i).set_read_start(pch);
249 m_curr_buff_idx = i;
250 break;
251 }
252 }
253 }
254
255 assert(from == SegmentationClassifier::get_slot_end(slot_idx));
256
258 m_last_completed_slot = slot_idx;
259}
260
262{
263 int32_t slot_idx = m_last_completed_slot;
264 if (-1 == slot_idx)
265 {
266 slot_idx = 0;
267 }
268 else
269 {
270 slot_idx = SegmentationClassifier::next_slot(slot_idx);
271 }
272
273 // We are in send_next_results
274 // Note, use get_lock_count from
275 // using SegmentationClassifier = RnnSequenceClassifier<eigen_impl::Model, eigen_impl::EmbdVectorizer, uint8_t> ;
276 // This one accesses its m_slots[idx].m_lock_count (std::vector<slot_t> member of RnnSequenceClassifier)
277 // while send_results (above but called below) do m_buff_set.get(i).unlock() (a
278 // locked_buffer_set_t), a member of SegmentationImpl
279 // Should we use SegmentationClassifier::decrement_lock_count in send_results too?
280
281 uint8_t lock_count = SegmentationClassifier::get_lock_count(slot_idx);
282
283 while (lock_count > 1)
284 {
285 // Worker still uses this slot. Waiting...
286 // std::cerr << "SegmentationImpl::send_next_results: waiting for slot " << slot_idx+1
287 // << " (lock_count==" << int(lock_count) << ")\n";
288 // m_buff_set.pretty_print();
289 // SegmentationClassifier::pretty_print();
291 lock_count = SegmentationClassifier::get_lock_count(slot_idx);
292 }
293 if (1 == lock_count)
294 {
295 // Data is ready. We can return it to caller
296 send_results(slot_idx);
297 }
298}
299
301{
303 {
305 // std::cerr << "SegmentationImpl::acquire_slot: got " << m_current_slot_no
306 // << " for timepoint " << m_current_timepoint << std::endl;
308
309 while (lock_count > 1)
310 {
311 // Worker still uses this slot. Waiting...
312 // std::cerr << "SegmentationImpl::acquire_slot, waiting for slot "
313 // << m_current_slot_no << " / " << lock_count << std::endl;
316 }
317 if (1 == lock_count)
318 {
319 // Data is ready. We can return it to caller
321 }
322
325 }
326}
327
329{
330 // std::cerr << "SegmentationImpl::handle_timepoint " << std::endl;
331 acquire_slot();
332
335
338 {
340 // std::cerr << "Slot " << m_current_slot_no << " sent to inference engine (segmentation)" << std::endl;
341 acquire_slot();
342 }
343}
344
353
355{
356 // std::cerr << "SegmentationImpl::finalize" << std::endl;
357
359 m_char_len.resize(SegmentationClassifier::size());
361 // no_more_data();
362 //
363 // for (size_t i = 0; i < m_buff_set.size(); i++)
364 // {
365 // locked_buffer_t& buff = m_buff_set.get(i);
366 // while (buff.locked())
367 // {
368 // send_next_results();
369 // // buff.unlock();
370 // }
371 // }
372 // for (auto i=0; i < SegmentationClassifier::get_num_slots(); i++)
373 // {
374 // while (SegmentationClassifier::get_lock_count(i) > 0)
375 // {
376 // SegmentationClassifier::decrement_lock_count(i);
377 // }
378 // }
379}
380
381} // namespace impl
382 // namespace lemmatization
383 // namespace deeplima
384
virtual void init(uint32_t max_feat, uint32_t overlap, uint32_t num_slots, uint32_t slot_len, uint32_t num_threads, bool precomputed_input=false)
size_t get_num_threads() const
Definition thread_pool.h:47
std::function< bool(uint8_t *buffer, int32_t &read, int32_t max) > read_callback_t
virtual void parse_from_stream(const read_callback_t fn) override
virtual void register_handler(const segmentation_callback_t fn) override
virtual void finalize() override
Cleanup all remaining locks if any.
void init(size_t threads, size_t buffer_size_per_thread)
virtual void load(const std::string &fn)
RnnSequenceClassifier< eigen_impl::Model, eigen_impl::EmbdVectorizer, uint8_t > SegmentationClassifier
std::function< void(const std::vector< token_pos > &tokens, uint32_t len) > segmentation_callback_t
locketd_buffer_t is the character-level buffer shared between the source file reader and the tokenize...
const char * m_char_aligned_data
bool locked() const