@@ -126,17 +126,8 @@ class EngineImpl : public Engine {
126
126
ModelWorkspace{model->AllocEmbeddingTensor (), model->AllocHiddenStatesTensor ()});
127
127
}
128
128
// - Initialize tokenizer and grammar
129
- n->tokenizer_ = Tokenizer::FromPath (engine_config->model );
130
- std::string token_table_postproc_method;
131
- if (model_configs[0 ].count (" token_table_postproc_method" ) == 0 ) {
132
- // Backward compatibility: use "byte_fallback" by default
133
- token_table_postproc_method = " byte_fallback" ;
134
- } else {
135
- token_table_postproc_method =
136
- model_configs[0 ].at (" token_table_postproc_method" ).get <std::string>();
137
- }
138
- n->token_table_ =
139
- Tokenizer::PostProcessTokenTable (n->tokenizer_ ->TokenTable (), token_table_postproc_method);
129
+ n->tokenizer_ = Tokenizer::FromPath (engine_config->model , GetTokenizerInfo (model_configs[0 ]));
130
+ n->token_table_ = n->tokenizer_ ->PostProcessedTokenTable ();
140
131
n->grammar_init_context_cache_ = GrammarInitContextCache (n->token_table_ );
141
132
// - Create the logit processor and sampler, and
142
133
// the DraftTokenWorkspaceManager for speculative decoding.
@@ -549,6 +540,28 @@ class EngineImpl : public Engine {
549
540
}
550
541
}
551
542
543
+ static std::optional<TokenizerInfo> GetTokenizerInfo (const picojson::object& model_config) {
544
+ if (model_config.count (" tokenizer_info" ) == 0 ) {
545
+ LOG (WARNING) << " Tokenizer info not found in mlc-chat-config.json. "
546
+ << " Trying to automatically detect the tokenizer info" ;
547
+ return std::nullopt ;
548
+ }
549
+ const picojson::object& tokenizer_info_obj =
550
+ model_config.at (" tokenizer_info" ).get <picojson::object>();
551
+ auto info = make_object<TokenizerInfoNode>();
552
+ if (tokenizer_info_obj.count (" token_postproc_method" )) {
553
+ info->token_postproc_method =
554
+ tokenizer_info_obj.at (" token_postproc_method" ).get <std::string>();
555
+ }
556
+ if (tokenizer_info_obj.count (" prepend_space_in_encode" )) {
557
+ info->prepend_space_in_encode = tokenizer_info_obj.at (" prepend_space_in_encode" ).get <bool >();
558
+ }
559
+ if (tokenizer_info_obj.count (" strip_space_in_decode" )) {
560
+ info->strip_space_in_decode = tokenizer_info_obj.at (" strip_space_in_decode" ).get <bool >();
561
+ }
562
+ return TokenizerInfo (info);
563
+ }
564
+
552
565
// Engine state, managing requests and request states.
553
566
EngineState estate_;
554
567
// Configurations and singletons
0 commit comments