-
Notifications
You must be signed in to change notification settings - Fork 0
/
translator.cpp
1388 lines (1297 loc) · 44.5 KB
/
translator.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "common.h"
#include "llama.h"
#include "main.hpp"
#include "sampling.h"
#include "tools/tiny_sha1.hpp"
#include <algorithm>
#include <array>
#include <atomic>
#include <chrono>
#include <cmath>
#include <condition_variable>
#include <deque>
#include <filesystem>
#include <fstream>
#include <iostream>
#include <map>
#include <mutex>
#include <numeric>
#include <thread>
#include <unordered_map>
#ifdef _MSC_VER
#include <intrin.h>
#else
#include <x86intrin.h>
#endif
#include <emmintrin.h>
#include <immintrin.h>
static_assert("か"sv == "\xE3\x81\x8B"sv, "This source file shall be compiled as UTF-8 text");
extern volatile bool g_stop;
extern std::string g_neg;
extern std::string example;
extern std::string iprefix;
extern std::string isuffix;
extern std::string cache_prefix;
extern std::string print_line(line_id id, std::string* line, bool stream);
extern void update_segment(uint seg, bool upd_names, uint count);
namespace fs = std::filesystem;
//__attribute__((target("avx2")))
double cosine_similarity(const std::vector<float>& id1, double sum1, const std::vector<float>& id2, double sum2)
{
double sum = 0.0;
std::size_t len = id1.size();
std::size_t i = 0;
#ifdef __SSE2__
__m128d acc0 = _mm_setzero_pd();
__m128d acc1 = _mm_setzero_pd();
for (auto n = len & -4; i < n; i += 4) {
auto v1 = _mm_load_ps(id1.data() + i);
auto v2 = _mm_load_ps(id2.data() + i);
auto v1l = _mm_cvtps_pd(v1);
auto v1h = _mm_cvtps_pd(_mm_movehl_ps(v1, v1));
auto v2l = _mm_cvtps_pd(v2);
auto v2h = _mm_cvtps_pd(_mm_movehl_ps(v2, v2));
acc0 = _mm_add_pd(acc0, _mm_mul_pd(v1l, v2l));
acc1 = _mm_add_pd(acc1, _mm_mul_pd(v1h, v2h));
}
double sum_pair[2];
_mm_storeu_pd(sum_pair, _mm_add_pd(acc0, acc1));
sum = sum_pair[0] + sum_pair[1];
#endif
for (auto n = id1.size(); i < n; i++) {
sum += static_cast<double>(id1[i]) * id2[i];
}
// Handle the case where one or both vectors are zero vectors
if (sum1 == 0.0 || sum2 == 0.0) {
if (sum1 == 0.0 && sum2 == 0.0) {
return 1.0; // two zero vectors are similar
}
return 0.0;
}
return sum / (sqrt(sum1) * sqrt(sum2));
}
decltype(g_replaces) make_name_replaces(std::string_view text)
{
static constexpr std::pair<std::array<const char*, 4>, std::array<const char*, 4>> s_suffix_map[]{
// clang-format off
{{"さん"}, {"-san"}},
{{"君", "くん"}, {"-kun"}},
{{"様", "さま", "しゃま", "ちゃま"}, {"-sama", "-shama", "-chama"}},
{{"ちゃん", "たん"}, {"-chan", "-tan"}},
{{"どの", "殿"}, {"-dono"}},
{{"先輩", "せんぱい", "センパイ"}, {"-senpai", "-sempai"}},
{{"先生", "せんせい", "センセイ"}, {"-sensei"}}
// clang-format on
};
decltype(g_replaces) result;
for (const auto& [orig_name, pair] : g_dict) {
// Filter non-names (TODO: this should be much more complicated)
const auto& [tr_name, tr_ann] = pair;
if (tr_name.empty() || tr_name.find_first_of("?& ") + 1)
continue;
std::string_view name = orig_name;
name -= ":";
std::size_t fpos = std::size(s_suffix_map), pos = 0;
std::vector<char> features(fpos + 1, 0);
while (pos < text.size()) {
// TODO: convert tr_name to hiragana/katakana and use for search as well
pos = text.find(name, pos);
if (pos + 1 == 0)
break;
pos += name.size();
bool found = false;
// Test each suffix in set
for (std::size_t f = 0; f < std::size(s_suffix_map); f++) {
for (const auto& suf : s_suffix_map[f].first) {
if (suf && text.substr(pos).starts_with(suf)) {
found = true;
features[f] |= 1;
fpos = f;
break;
}
}
}
if (!found)
features.back() |= 1;
}
// Conflicts or nothing found
if (std::accumulate(features.begin(), features.end(), 0) != 1)
continue;
std::string from(tr_name - ":");
std::string to(from);
if (!features.back()) {
to += s_suffix_map[fpos].second[0];
result.emplace_back(from, to);
} else {
// Disable "remove all suffixes" for now
// Add dummy replace to indicate the presence of the name
result.emplace_back(from, from);
continue;
}
for (std::size_t f = 0; f < std::size(s_suffix_map); f++) {
if (!features[f]) {
for (const auto& tr_sfx : s_suffix_map[f].second) {
if (tr_sfx)
result.emplace_back(from + tr_sfx, to);
}
}
}
}
return result;
}
// Return true if some meaningful CJK character is found (TODO: potentially incomplete)
bool check_cjk_line(std::u16string_view sq_text)
{
for (char16_t c : sq_text) {
// clang-format off
if ((c >= '0' && c <= '9') || // ASCII numbers and letters are included too
(c >= 'a' && c <= 'z') ||
(c >= 'A' && c <= 'Z') ||
(c >= 0x3040 && c <= 0x30ff) || // Hiragana, katakana
(c >= 0x4e00 && c <= 0x9fff) || // CJK ideograms
(c >= 0xf900 && c <= 0xfaff) ||
(c >= 0xac00 && c <= 0xd7af) || // Hangul precomposed
(c >= 0x1100 && c <= 0x11ff) || // Hangul Jamo
(c >= 0x3130 && c <= 0x318f) ||
(c >= U'0' && c <= U'9') ||
(c >= U'A' && c <= U'Z') ||
(c >= U'a' && c <= U'z') ||
(c >= 0xff65 && c <= 0xff9f) || // Halfwidth kana
c == '-')
return true;
// clang-format on
}
return false;
}
// Workaround for llama.cpp tokenize which expects correct utf-8
bool is_valid_utf8(std::string_view str)
{
uint cont = 0;
for (const char& c : str) {
if ((c & 0x80) == 0) {
// ASCII
if (cont)
return false;
} else if ((c & 0xc0) == 0x80) {
if (!cont)
return false;
cont--;
} else if (cont) {
return false;
} else if ((c & 0xe0) == 0xc0) {
cont = 1;
} else if ((c & 0xf0) == 0xe0) {
cont = 2;
} else if ((c & 0xf8) == 0xf0) {
cont = 3;
} else {
return false;
}
}
return !cont;
}
std::vector<std::pair<uint, float>> get_recollections(common_params& params, line_id id, std::size_t pos_max)
{
if (pos_max >= g_history.size()) {
// Handle underflow from subtraction
return {};
}
if (params.speculative.model == "." || !check_cjk_line(g_lines[id].sq_text)) {
// Don't process lines like "..."
return {};
}
// Vector of history positions
std::vector<std::pair<uint, float>> result;
auto& line = g_lines[id];
// Relevancy mapping
struct rel_ref {
std::size_t pos;
double sim;
};
std::deque<rel_ref> rel_map;
for (std::size_t i = 0; i <= pos_max; i++) {
// TODO: filter repetitions in the cause of history loops
if (!check_cjk_line(g_lines[g_history[i]].sq_text))
continue;
auto& hline = g_lines[g_history[i]];
// Exclude too big lines (TODO)
if (hline.tokens > std::min(255u, params.n_ctx / 8u / 4u))
continue;
// More recent history appears first
auto& rel = rel_map.emplace_front();
rel.pos = i;
rel.sim = cosine_similarity(line.embd, line.embd_sqrsum, hline.embd, hline.embd_sqrsum);
}
std::stable_sort(rel_map.begin(), rel_map.end(), [](const rel_ref& a, const rel_ref& b) {
// Sort by similarity in descending order
return a.sim > b.sim;
});
auto end = std::unique(rel_map.begin(), rel_map.end(), [](const rel_ref& a, const rel_ref& b) {
// Filter duplicates (TODO: is it good idea?)
// There is also a strange issue when equal strings get slightly different embeddings.
auto& linea = g_lines[g_history[a.pos]];
auto& lineb = g_lines[g_history[b.pos]];
return &linea == &lineb || (linea.name == lineb.name && linea.text == lineb.text);
});
rel_map.erase(end, rel_map.end());
uint tokens = 0;
for (auto& rel : rel_map) {
// Cut elements with low similarity
auto& [pos, sim] = rel;
tokens += g_lines[g_history[pos]].tokens;
if (tokens > params.n_ctx / 8u - 1) {
break;
}
result.emplace_back(pos, sim);
}
// Sort result by history order
std::sort(result.begin(), result.end());
return result;
}
std::vector<llama_token> llama_tokenize(llama_model* model, std::string_view str, bool add_special, bool parse_special = false)
{
std::vector<llama_token> result;
result.resize(llama_n_ctx_train(model));
auto count = llama_tokenize(model, str.data(), str.size(), result.data(), result.size(), add_special, parse_special);
if (count < 0) {
result.resize(-count);
if (-count != llama_tokenize(model, str.data(), str.size(), result.data(), result.size(), add_special, parse_special))
throw std::runtime_error("llama_tokenize");
} else {
result.resize(count);
}
return result;
}
std::string llama_token_to_piece(llama_model* model, llama_token t, bool special = true)
{
std::string result;
result.resize(1024);
result.resize(llama_token_to_piece(model, t, result.data(), result.size(), 0, special));
return result;
}
bool translate(common_params& params, line_id id, tr_cmd cmd)
{
static const auto s_main_tid = std::this_thread::get_id();
static std::atomic<uint> stop_sig = -1; // stop signal for thread, id.second to start discarding from
static std::atomic<uint> work_res = 0; // number of translated lines in segment, done by worker
static std::condition_variable work_cv;
static std::thread worker;
static auto is_stopped = [](line_id id = c_bad_id) -> bool {
if (g_stop)
return true;
if (std::this_thread::get_id() != s_main_tid) {
if (uint sig = stop_sig.load(); sig + 1) {
// Compare discard start pos with current id
if (id.second >= sig)
return true;
}
}
return false;
};
static auto join_worker = [](uint val) {
if (worker.joinable()) {
std::lock_guard{g_mutex}, stop_sig = val;
work_cv.notify_all();
worker.join();
}
};
// Initialize llama.cpp
static const auto init_result = [&]() -> common_init_result {
llama_backend_init();
llama_numa_init(params.numa);
return common_init_from_params(params);
}();
static const auto model = init_result.model;
static const auto ctx = init_result.context;
// Load embedding model if specified as "draft model"
static const auto init_result_e = [&]() -> common_init_result {
if (params.speculative.model == ".")
return {nullptr, nullptr, {}};
if (params.speculative.model.empty())
return init_result;
auto eparams = params;
eparams.model = std::move(params.speculative.model);
eparams.n_gpu_layers = params.speculative.n_gpu_layers;
eparams.lora_adapters.clear();
eparams.embedding = true;
eparams.n_ctx = 0; // auto
eparams.n_ubatch = 512; //def
eparams.n_batch = 2048; //def
eparams.pooling_type = LLAMA_POOLING_TYPE_NONE;
eparams.flash_attn = true;
eparams.cache_type_k = "f16";
eparams.cache_type_v = "f16";
return common_init_from_params(eparams);
}();
static const auto emodel = init_result_e.model;
static const auto ectx = init_result_e.context;
static std::vector<llama_token> tokens; // Current tokens (not necessarily decoded)
static std::deque<uint> chunks; // Size (in tokens) of each translated message block
static uint decoded = 0; // Number of token successfully llama_decode()'d
static uint segment = -1; // Current segment
static uint hist_pos = -1; // Position in g_history corresponding to chunks[0]
static uint prompt_size = 0; // Number of prompt tokens + reserved area for recollections
static float penalty_scale = 1.f;
static std::map<line_id, std::vector<std::uint8_t>> recollections;
// Something like "$HOME/.cache/VNSleuth/GameXXX"
static const fs::path s_cache_path = []() {
fs::path result;
#if defined(__APPLE__)
result = std::getenv("HOME");
result /= "Library";
result /= "Caches";
#elif defined(_WIN32)
result = std::getenv("LOCALAPPDATA");
#else
if (auto v = std::getenv("XDG_CACHE_HOME")) {
result = v;
} else {
result = std::getenv("HOME");
result /= ".cache";
}
#endif
result /= "VNSleuth";
return result;
}() / cache_prefix;
static const struct _init_t {
explicit operator bool() const { return model && ctx; }
_init_t(common_params&)
{
if (!model || !ctx)
return;
if (llama_model_has_encoder(model))
throw std::runtime_error("Decoder-only model expected");
fs::create_directories(s_cache_path / "__embd");
}
~_init_t()
{
join_worker(0);
if (ectx && ectx != ctx)
llama_free(ectx);
if (emodel && emodel != model)
llama_free_model(emodel);
llama_free(ctx);
llama_free_model(model);
llama_backend_free();
}
} _init{params};
if (!_init) {
std::cerr << "Failed to initialize llama model." << std::endl;
return false;
}
if (cmd == tr_cmd::sync) {
// Abort background worker and possibly discard work
// Send Id to start discarding from, obviously can't use -1
join_worker(std::min<uint>(id.second, -2));
return true;
}
if (cmd == tr_cmd::kick && std::this_thread::get_id() == s_main_tid) {
// Stop background worker first
if (segment == id.first && worker.joinable()) {
// Kick only once last translated line has been read
uint tr_lines = work_res;
uint rd_lines = id.second + 1;
// Compare translated count with the number of read lines
if (tr_lines > rd_lines) {
// Only notify worker
work_cv.notify_all();
return true;
} else if (tr_lines != rd_lines) {
std::fprintf(stderr, "%sError: Kicked from untranslated line: %u<%u\n", g_esc.reset, tr_lines, rd_lines);
}
}
join_worker(id.first != segment ? 0 : id.second + 1);
}
// Remove first message block (returns number of tokens to erase)
static auto eject_first = [¶ms]() -> uint {
if (chunks.empty()) {
return 0;
}
auto count = chunks.front();
chunks.pop_front();
if (decoded > prompt_size) {
// std::cerr << "*Used cells: " << llama_get_kv_cache_used_cells(ctx) << std::endl;
// std::cerr << "*Decoded: " << decoded << std::endl;
// std::cerr << "*Tokens: " << tokens.size() << std::endl;
const auto p0 = prompt_size;
const auto p1 = prompt_size + count;
if (!llama_kv_cache_seq_rm(ctx, 0, p0, p1))
throw std::runtime_error("llama_kv_cache_seq_rm 1");
llama_kv_cache_seq_add(ctx, 0, p1, -1, 0u - count);
decoded -= count;
if (llama_get_kv_cache_used_cells(ctx) + 0u != decoded) {
std::cerr << "Used cells: " << llama_get_kv_cache_used_cells(ctx) << std::endl;
std::cerr << "Decoded: " << decoded << std::endl;
// llama_kv_cache_view view = llama_kv_cache_view_init(ctx, 2);
// llama_kv_cache_view_update(ctx, &view);
// llama_kv_cache_dump_view(view);
throw std::runtime_error("used cells overflow");
}
if (decoded > 0u + params.n_ctx)
throw std::out_of_range("ctx underflow");
if (decoded < prompt_size)
throw std::out_of_range("eject_first failed");
}
hist_pos++;
return count;
};
static const auto unload = []() {
uint count = 0;
while (!chunks.empty()) {
count += eject_first();
}
tokens.clear();
recollections.clear();
decoded = 0;
llama_kv_cache_clear(ctx);
hist_pos = 0;
};
// Remove last message block(s)
static auto eject_bunch = [¶ms](uint i, [[maybe_unused]] bool locked = true) {
if (i > chunks.size()) {
unload();
if (llama_add_bos_token(model)) {
tokens.push_back(llama_token_bos(model));
}
std::string buf;
buf.resize(256);
buf.resize(llama_model_desc(model, buf.data(), buf.size()));
if (buf.starts_with("command-r ")) {
penalty_scale = 3.f;
}
tokens += llama_tokenize(model, params.prompt + example, false);
params.n_keep = tokens.size();
prompt_size = tokens.size();
if (ectx) {
// Make space for recollections (currently constant)
prompt_size += params.n_ctx / 8u;
tokens.resize(prompt_size, llama_token_nl(model));
}
std::cerr << "Permanent tokens: " << prompt_size << std::endl;
return;
}
while (i--) {
auto count = chunks.back();
chunks.pop_back();
if (decoded == tokens.size()) {
if (!llama_kv_cache_seq_rm(ctx, 0, decoded - count, -1))
throw std::runtime_error("llama_kv_cache_seq_rm last");
if (llama_get_kv_cache_used_cells(ctx) + 0u != tokens.size() - count)
throw std::runtime_error("used cells in eject_bunch");
decoded -= count;
if (decoded > tokens.size() || decoded < prompt_size)
throw std::out_of_range("eject_bunch decoded=" + std::to_string(decoded));
}
tokens.resize(tokens.size() - count);
g_stats->raw_discards++;
}
};
// Eject old translations if necessary
static auto eject_start = [¶ms](bool defrag = false) -> bool {
uint count = 0;
while (tokens.size() - count + params.n_predict * 2 > params.n_ctx - 1u) {
if (chunks.empty()) {
std::cerr << "Prompt too big or context is too small" << std::endl;
return false;
}
count += eject_first();
}
tokens.erase(tokens.begin() + prompt_size, tokens.begin() + (prompt_size + count));
// Apply defrag
if (defrag) {
llama_kv_cache_defrag(ctx);
llama_kv_cache_update(ctx);
}
return true;
};
// Tokenize line and add to the tokens
static auto push_str = [¶ms](const std::string& text, bool front = false, bool spec = false) -> uint {
auto tt = llama_tokenize(model, text, false, spec);
if (params.verbosity && !tt.empty())
std::fprintf(stderr, "%s[tokens:%zu,%d] push %zu tokens: '%s'\n", g_esc.reset, tokens.size(), +front, tt.size(), text.c_str());
if (front) {
tokens.insert(tokens.begin() + prompt_size + chunks.front(), tt.begin(), tt.end());
chunks.front() += tt.size();
} else {
tokens.insert(tokens.end(), tt.begin(), tt.end());
chunks.back() += tt.size();
}
return tt.size();
};
// Tokenize tr_text from id
static auto push_id = [¶ms](line_id id, bool front = false) {
auto tr_text = std::string_view(g_lines[id].tr_text);
auto spos = tr_text.find("\n" + isuffix) + 1;
if (!spos) {
throw std::runtime_error("Line untranslated: " + g_lines[id].tr_text);
}
// Tokenize original strings with annotations
std::size_t pref_pos = 0, post_pos = 0;
if (!tr_text.starts_with(iprefix)) {
post_pos = pref_pos = tr_text.find("\n" + iprefix) + 1;
if (pref_pos == 0)
throw std::runtime_error("Line without iprefix: " + g_lines[id].tr_text);
}
// Replay print_line logic
auto out = apply_replaces(g_lines[id].text, false, 0);
// Pre-annotations: no replaces, then original line with replaces
g_lines[id].pre_ann = tr_text.substr(0, pref_pos);
out = g_lines[id].pre_ann + iprefix + g_lines[id].name + std::move(out) + "\n";
// Post-annotations: no replaces
post_pos += iprefix.size();
post_pos += g_lines[id].name.size();
post_pos += g_lines[id].text.size() + 1;
g_lines[id].post_ann = tr_text.substr(post_pos, spos - post_pos);
out += g_lines[id].post_ann;
out += isuffix;
push_str(out, front);
tr_text.remove_prefix(spos);
tr_text.remove_prefix(isuffix.size());
int token_count = 0;
if (!g_lines[id].name.empty()) {
// Tokenize name separately: find ": " delimiter
auto pos = tr_text.find(": ") + 1;
if (!pos) {
throw std::runtime_error("Name untranslated: " + g_lines[id].tr_text);
}
if (isuffix.ends_with(" "))
pos++;
token_count += push_str(std::string(tr_text.substr(0, pos)), front);
tr_text.remove_prefix(pos);
}
token_count += push_str(std::string(tr_text), front);
g_lines[id].tr_tts.clear();
g_lines[id].tr_tts.emplace_back(tokens.end() - token_count, tokens.end());
if (token_count > params.n_predict + 1)
throw std::runtime_error("Line too long: " + g_lines[id].tr_text);
g_lines[id].tokens = front ? chunks.front() : chunks.back();
};
static auto decode_internal = [¶ms](uint count) -> void {
if (params.verbosity) {
std::cerr << "Decoding:" << g_esc.buf;
auto end_it = tokens.begin() + decoded;
if (tokens.end() - end_it > 300)
end_it += 300;
else
end_it = tokens.end();
for (auto it = tokens.begin() + decoded; it != end_it; it++) {
auto str = llama_token_to_piece(model, *it, false);
REPLACE(str, "\n", "\\n");
std::cerr << str;
}
std::cerr << g_esc.reset << std::endl;
}
auto stamp0 = std::chrono::steady_clock::now();
uint total = 0;
if (count >= 0u + params.n_ctx)
throw std::runtime_error("decode(): too many tokens: " + std::to_string(count));
while (uint bsize = std::min<uint>(count, params.n_batch)) {
// TODO: cannot properly interrupt by is_stopped, but probably not relevant anymore
auto res = llama_decode(ctx, llama_batch_get_one(&tokens[decoded], bsize));
if (res == 1) {
llama_kv_cache_defrag(ctx);
continue;
}
if (res < 0) {
throw std::runtime_error("decode failed");
}
decoded += bsize;
total += bsize;
count -= bsize;
}
llama_synchronize(ctx);
g_stats->raw_decodes += total;
auto stamp1 = std::chrono::steady_clock::now();
if (total > 1) {
g_stats->batch_count += total;
g_stats->batch_time += (stamp1 - stamp0).count() / 1000;
}
};
static auto decode = [¶ms](line_id id, uint injected = 0) -> void {
if (tokens.size() == decoded)
return;
if (decoded == 0) {
// Decode prompt first
decode_internal(prompt_size);
} else if (injected) {
if (id == c_bad_id) {
std::fill(tokens.begin() + params.n_keep, tokens.begin() + prompt_size, llama_token_nl(model));
}
llama_kv_cache_seq_rm(ctx, 0, params.n_keep, prompt_size);
auto batch = llama_batch_init(injected, 0, 1);
for (uint i = 0; i < injected; i++) {
common_batch_add(batch, tokens[params.n_keep + i], params.n_keep + i, {0}, false);
}
if (llama_decode(ctx, batch) != 0)
throw std::runtime_error("llama_decode failed (injected)");
llama_batch_free(batch);
llama_synchronize(ctx);
llama_kv_cache_seq_cp(ctx, 0, 1, params.n_keep, prompt_size);
if (id != c_bad_id) {
// Limit recollections cache
while (recollections.size() >= params.speculative.n_min * 2) {
if (recollections.begin()->first > id)
recollections.erase(std::prev(recollections.end()));
else
recollections.erase(recollections.begin());
}
// Cache recollections
std::vector<std::uint8_t> buf;
buf.resize(llama_state_seq_get_size(ctx, 1));
buf.resize(llama_state_seq_get_data(ctx, buf.data(), buf.size(), 1));
recollections[id] = std::move(buf);
}
llama_kv_cache_seq_keep(ctx, 0);
}
decode_internal(tokens.size() - decoded);
if (llama_get_kv_cache_used_cells(ctx) + 0u != decoded) {
throw std::runtime_error("used cells after decode");
}
};
static auto inject_recollections = [¶ms](line_id id) -> uint {
if (!ectx)
return 0;
const auto found = recollections.find(id);
if (found != recollections.end()) {
llama_kv_cache_seq_rm(ctx, 0, params.n_keep, prompt_size);
llama_state_seq_set_data(ctx, found->second.data(), found->second.size(), 1);
llama_kv_cache_seq_cp(ctx, 1, 0, params.n_keep, prompt_size);
llama_kv_cache_seq_keep(ctx, 0);
return 0;
}
std::lock_guard lock(g_mutex);
uint injected = 0;
std::fill(tokens.begin() + params.n_keep, tokens.begin() + prompt_size, llama_token_nl(model));
if (auto inj_list = get_recollections(params, id, hist_pos - 1); !inj_list.empty()) {
for (auto it = inj_list.begin(); it != inj_list.end(); it++) {
auto iid = g_history[it->first];
auto tts = llama_tokenize(model, g_lines[iid].tr_text, false);
std::copy(tts.begin(), tts.end(), tokens.begin() + params.n_keep + injected);
injected += tts.size();
}
}
// Return constant number of tokens to decode
return prompt_size - params.n_keep;
};
// Compose embedding cache filename from the hash of prompt+text
static auto get_embd_file = [](line_id id) -> std::string {
std::string result;
char buf[42]{};
auto& line = g_lines[id];
{
sha1::SHA1 s;
int info[] = {llama_n_embd(emodel), llama_n_head(emodel), llama_n_layer(emodel), llama_n_vocab(emodel)};
s.processBytes(&info, sizeof(info));
//s.processBytes(line.name.data(), line.name.size());
s.processBytes(line.text.data(), line.text.size());
std::uint32_t digest[5];
s.getDigest(digest);
std::snprintf(buf, 41, "%08x%08x%08x%08x%08x", digest[0], digest[1], digest[2], digest[3], digest[4]);
}
result += buf;
result += ".embf32";
return s_cache_path / "__embd" / result;
};
static auto make_embedding = [](line_id id) -> void {
auto esize = llama_n_embd(emodel);
uint tsize = 0;
std::vector<std::string> paths;
std::vector<line_info*> lines;
std::vector<std::vector<int>> tokens;
// Try to batch as many embeddings as possible
while (id != c_bad_id) {
auto fname = get_embd_file(id);
auto& line = g_lines[id];
g_lines.advance(id);
if (!line.embd.empty())
continue;
auto tt = llama_tokenize(emodel, "query: " + line.text, true);
if (tt.size() > llama_n_ctx(ectx) - 1u)
tt.resize(llama_n_ctx(ectx) - 1u);
if (tsize + tt.size() > llama_n_ctx(ectx) - 1u)
break;
tsize += tt.size();
paths.emplace_back(std::move(fname));
lines.emplace_back(&line);
tokens.emplace_back(std::move(tt));
line.embd.resize(esize, 0.f);
}
if (paths.empty())
return;
llama_set_embeddings(ectx, true);
llama_set_causal_attn(ectx, false);
auto batch = llama_batch_init(llama_n_ctx(ectx), 0, 1);
for (uint i = 0; i < lines.size(); i++) {
for (uint j = 0; j < tokens[i].size(); j++) {
common_batch_add(batch, tokens[i][j], j, {llama_seq_id(i + 1)}, true);
}
}
if (llama_decode(ectx, batch) != 0)
throw std::runtime_error("llama_decode failed (embd)");
for (uint k = 0; k < tsize; k++) {
// Use simple sum of each token's embeddings (TODO)
auto* embd = llama_get_embeddings_ith(ectx, k);
auto* line = lines[batch.seq_id[k][0] - 1];
for (float& x : line->embd)
x += *embd++;
}
llama_batch_free(batch);
llama_kv_cache_seq_keep(ectx, 0);
llama_set_embeddings(ectx, false);
llama_set_causal_attn(ectx, true);
// Save embeddings
for (uint i = 0; i < paths.size(); i++) {
auto& line = *lines[i];
std::ofstream file(paths[i] + "~", std::ios::binary | std::ios::trunc);
if (!file.is_open()) {
throw std::runtime_error("Failed to create " + paths[i] + "~");
}
file.write(reinterpret_cast<char*>(line.embd.data()), esize * sizeof(float));
file.close();
fs::rename(paths[i] + "~", paths[i]);
// Precompute ||embd||
line.embd_sqrsum = 0;
for (int i = 0; i < esize; i++) {
line.embd_sqrsum += double(line.embd[i]) * line.embd[i];
}
}
};
static auto load_embedding = [](line_id id) -> void {
if (!ectx)
return;
uint esize = llama_n_embd(emodel);
auto fname = get_embd_file(id);
auto& line = g_lines[id];
std::ifstream file(fname, std::ios::binary);
if (!file.is_open()) {
make_embedding(id);
} else {
line.embd.resize(esize);
line.embd_sqrsum = 0;
file.read(reinterpret_cast<char*>(line.embd.data()), esize * sizeof(float));
if (file.tellg() * sizeof(char) != esize * sizeof(float))
throw std::runtime_error("Truncated embd file " + fname);
for (uint i = 0; i < esize; i++) {
line.embd_sqrsum += double(line.embd[i]) * line.embd[i];
}
}
};
static auto init_segment = [&]() -> void {
// Initialize segment: eject all first
eject_bunch(-1);
decode(c_bad_id);
// Invalidate embeddings
for (auto& line : g_lines) {
line.embd.clear();
line.embd_sqrsum = 0.;
}
if (std::getenv("VNSLEUTH_DUMP_SIMILARITY")) {
g_history.clear();
std::ofstream dump(s_cache_path / (ctx == ectx ? "hdumpz.txt" : "hdump.txt"), std::ios_base::trunc);
dump << std::fixed;
std::cerr << std::endl;
for (auto& line : g_lines) {
auto id = line.get_id();
g_history.push_back(id);
line.tokens = llama_n_ctx(ectx) / 32;
load_embedding(id);
auto recs = get_recollections(params, id, g_history.size() - 2);
std::sort(recs.begin(), recs.end(), [](auto& a, auto& b) { return a.second > b.second; });
dump << "Line: " << g_lines.segs[id.first].src_name << ":" << id.second << std::endl;
for (auto& [pos, sim] : recs) {
auto line0 = g_lines[g_history[pos]];
dump << "\t[" << sim << "] " << line0.name << line0.text << std::endl;
}
dump << "\t[->] " << line.name << line.text << std::endl;
std::cerr << "\r" << g_history.size() << std::flush;
}
std::cerr << std::endl;
dump.close();
std::exit(0);
}
// Load full history
for (auto& id : g_history) {
if (g_lines[id].tr_text.empty())
break;
chunks.emplace_back();
push_id(id);
// Check cache files
load_embedding(id);
}
if (!eject_start())
throw std::runtime_error("eject_start init_seg");
decode(c_bad_id);
};
auto add_tail_finalize = [id]() -> void {
if (segment + 1) {
auto& tail = g_lines.segs[segment].tr_tail;
if (id.second == 0 && !g_lines.segs[segment].lines.back().tr_text.empty()) {
// Finalize segment if necessary (TODO: try make it atomic with next segment creation)
if (tail.empty()) {
tail = "\n";
update_segment(segment, true, -1);
}
}
}
};
if (cmd == tr_cmd::eject) {
eject_bunch(id.second);
return true;
}
if (cmd == tr_cmd::unload) {
unload();
return true;
}
if (cmd == tr_cmd::reload) {
if (id == c_bad_id || !decoded || (decoded - prompt_size) < (params.n_ctx - prompt_size) / 3u) {
// Full reload
init_segment();
return true;
}
if (id.first != segment) {
add_tail_finalize();
segment = id.first;
}
if (segment + 1) {
for (line_id nid{segment, id.second}; g_lines.is_valid(nid); g_lines.advance(nid)) {
if (g_lines[nid].tr_text.empty())
break;
chunks.emplace_back();
push_id(nid);
load_embedding(nid);
if (!eject_start())
throw std::runtime_error("eject_start reload");
decode(nid);
}
}
return true;
}
if (id.first != segment) {
// Update previous translation file
if (segment + 1) {
add_tail_finalize();
}
segment = id.first;
if (tokens.empty())
init_segment();
}
// Translate line(s)
if (id != c_bad_id) {
// Discard current and following lines
uint to_eject = 0;
for (line_id nid = id; cmd == tr_cmd::translate && nid != c_bad_id; g_lines.advance(nid)) {
std::lock_guard lock(g_mutex);
if (g_lines[nid].tr_text.empty())
break;
to_eject++;
if (params.verbosity)
std::fprintf(stderr, "%s[id:%u:%u] Ejected\n", g_esc.reset, nid.first, nid.second);
g_lines[nid].tr_text = {};
if (g_lines[nid].tr_tts.empty())
throw std::runtime_error("tr_tts not found: " + g_lines[nid].text);
// tr_tts is not discarded here
}
eject_bunch(to_eject);
} else {
return true;
}
// Detect additional lines for translation
bool echo = false;
for (line_id pid{segment, 0}; cmd == tr_cmd::translate && pid <= id; g_lines.advance(pid)) {
auto& line = g_lines[pid];
if (!(std::lock_guard{g_mutex}, line.tr_text.empty()))
continue;
auto lag0 = std::chrono::steady_clock::now();
std::string llama_out;
llama_out += line.pre_ann;
if (pid < id || line.tr_tts.empty())
echo = true; // Sticky flag to display source line with annotations, otherwise this is rewrite request
if (std::this_thread::get_id() == s_main_tid && echo)
std::cout << g_esc.orig << line.pre_ann << std::flush;
std::string spker = print_line(pid, &llama_out, std::this_thread::get_id() == s_main_tid && echo);
if (std::this_thread::get_id() == s_main_tid && echo)
std::cout << g_esc.orig << line.post_ann << std::flush;
llama_out += line.post_ann;
llama_out += isuffix;
chunks.emplace_back();
push_str(llama_out);
// Encode speaker separately and count its tokens
int pred_count = 0;
// Process rewrite request
if (std::this_thread::get_id() == s_main_tid && !g_neg.empty()) {
// Copy previous translation's tokens preceding selection
llama_out.clear();
std::vector<std::string> tstrs;
for (auto t : line.tr_tts.back()) {