yum/FastTextPager
Compressed text paging over OSC.
git clone https://git.yummers.dev/yum/FastTextPager
0c54e1f
master
1import math 2import sentencepiece as spm 3 4def get_tokenizer (): 5model_path = "./custom_unigram_tokenizer_65k/unigram.model" 6sp = spm .SentencePieceProcessor () 7sp .load (model_path ) 8return sp 9 10tokenizer = get_tokenizer () 11 12f"vocabulary size: { tokenizer . get_piece_size () } " ) 13# Sentencepiece uses U+2581 (lower one eighth block) to indicate a space before 14# a subword. 15sp_space = chr (9601 ) 16tokens_with_non_ascii = set () 17subword_len_histo = dict () 18# The sum of the lengths of each subword in the vocabulary. These are rounded 19# up to 4 characters. 20vocab_len_4c_quantized = 0 21 22for i in range (tokenizer .get_piece_size ()): 23k = tokenizer .id_to_piece (i ) 24v = i 25f" Original token ( { v } ): { repr ( k ) } ( { ' ' . join ( str ( ord ( k_c )) for k_c in k ) } )" ) 26for k_c in k : 27if ord (k_c )> 127 and ord (k_c )!= 9601 : 28tokens_with_non_ascii .add (k ) 29break 30k_processed = k .replace (sp_space ,' ' ) 31if not k .startswith (sp_space )and k not in ["[UNK]" ,"[PAD]" ,"[CLS]" ,"[SEP]" ,"[MASK]" ]: 32k_processed = k 33else : 34k_processed = k_processed 35 36current_len = len (k_processed ) 37if current_len in subword_len_histo : 38subword_len_histo [current_len ]+= 1 39else : 40subword_len_histo [current_len ]= 1 41 42vocab_len_4c_quantized += math .ceil (current_len / 4.0 )* 4.0 43f" { v } : { k_processed } " ) 44 45f"Num tokens with non-ascii: { len ( tokens_with_non_ascii ) } ( { 100 * len ( tokens_with_non_ascii ) / tokenizer . get_piece_size ():.2f } )%" ) 46 47f"Subword length histogram:" ) 48avg_subword_len = 0 49total_pieces_for_avg = 0 50for k_len ,v_count in sorted (subword_len_histo .items (),key = lambda x :x [0 ]): 51avg_subword_len += k_len * v_count 52total_pieces_for_avg += v_count 53f" { k_len } : { v_count } " ) 54 55if total_pieces_for_avg > 0 : 56avg_subword_len /= total_pieces_for_avg 57f"Average subword length: { avg_subword_len :.4f } " ) 58else : 59"Average subword length: N/A (no pieces analyzed)" ) 60 61f"Sum of all subword lengths, quantized to 4 character chunks: { vocab_len_4c_quantized } " )