8
8
ExLlamaV2TokenizerSPM ,
9
9
ExLlamaV2TokenizerHF
10
10
)
11
+ import threading
12
+
13
+
14
+ lock = threading .RLock ()
15
+ def synchronized_init (func ):
16
+ def wrapper (* args , ** kwargs ):
17
+ with lock :
18
+ return func (* args , ** kwargs )
19
+ return wrapper
20
+
11
21
12
22
class ExLlamaV2Tokenizer :
13
23
@@ -20,7 +30,6 @@ def __init__(self, children = None, leaf = None):
20
30
self .children = children if children is not None else {}
21
31
self .leaf = leaf if leaf is not None else []
22
32
23
-
24
33
config : ExLlamaV2Config
25
34
26
35
tokenizer_model : ExLlamaV2TokenizerBase
@@ -567,8 +576,8 @@ def num_tokens(self, text):
567
576
568
577
# Get ordinals of single-byte tokens
569
578
579
+ @synchronized_init
570
580
def get_id_to_ord_list (self ):
571
-
572
581
if self .id_to_ord is not None : return self .id_to_ord
573
582
574
583
self .id_to_ord = []
@@ -594,6 +603,7 @@ def get_id_to_ord_list(self):
594
603
595
604
# Copy vocabulary from model
596
605
606
+ @synchronized_init
597
607
def get_id_to_piece_list (self , include_special_tokens = False ):
598
608
599
609
if include_special_tokens :
@@ -633,6 +643,7 @@ def get_id_to_piece_list(self, include_special_tokens = False):
633
643
return self .id_to_piece
634
644
635
645
646
+ @synchronized_init
636
647
def get_piece_to_id_dict (self ):
637
648
638
649
if self .piece_to_id is not None : return self .piece_to_id
@@ -644,6 +655,7 @@ def get_piece_to_id_dict(self):
644
655
645
656
# Create dictionary mapping prefixes to token IDs
646
657
658
+ @synchronized_init
647
659
def get_prefix_to_ids_dict (self ):
648
660
649
661
if self .prefix_to_ids is not None : return self .prefix_to_ids
@@ -671,6 +683,7 @@ def get_prefix_to_ids_dict(self):
671
683
672
684
# Create dictionary mapping each ID to any IDs that it prefixes
673
685
686
+ @synchronized_init
674
687
def get_prefix_id_to_ids_dict (self ):
675
688
676
689
if self .prefix_id_to_ids is not None : return self .prefix_id_to_ids
@@ -712,6 +725,7 @@ def _make_trie(self, ci):
712
725
return trie
713
726
714
727
728
+ @synchronized_init
715
729
def get_char_trie (self ):
716
730
717
731
if self .char_trie is not None : return self .char_trie
@@ -720,6 +734,7 @@ def get_char_trie(self):
720
734
return self .char_trie
721
735
722
736
737
+ @synchronized_init
723
738
def get_char_trie_ci (self ):
724
739
725
740
if self .char_trie_ci is not None : return self .char_trie_ci
0 commit comments