MCPcopy Create free account
hub / github.com/Tiiny-AI/PowerInfer / process_token

Method process_token

smallthinker/tools/server/server.cpp:2232–2382  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

2230 }
2231
2232 bool process_token(completion_token_output & result, server_slot & slot) {
2233 // remember which tokens were sampled - used for repetition penalties during sampling
2234 const std::string token_str = result.text_to_send;
2235 slot.sampled = result.tok;
2236
2237 slot.generated_text += token_str;
2238 if (slot.params.return_tokens) {
2239 slot.generated_tokens.push_back(result.tok);
2240 }
2241 slot.has_next_token = true;
2242
2243 // check if there is incomplete UTF-8 character at the end
2244 bool incomplete = validate_utf8(slot.generated_text) < slot.generated_text.size();
2245
2246 // search stop word and delete it
2247 if (!incomplete) {
2248 size_t pos = std::min(slot.n_sent_text, slot.generated_text.size());
2249
2250 const std::string str_test = slot.generated_text.substr(pos);
2251 bool send_text = true;
2252
2253 size_t stop_pos = slot.find_stopping_strings(str_test, token_str.size(), true);
2254 if (stop_pos != std::string::npos) {
2255 slot.generated_text.erase(
2256 slot.generated_text.begin() + pos + stop_pos,
2257 slot.generated_text.end());
2258 pos = std::min(slot.n_sent_text, slot.generated_text.size());
2259 } else if (slot.has_next_token) {
2260 stop_pos = slot.find_stopping_strings(str_test, token_str.size(), false);
2261 send_text = stop_pos == std::string::npos;
2262 }
2263
2264 // check if there is any token to predict
2265 if (send_text) {
2266 // no send the stop word in the response
2267 result.text_to_send = slot.generated_text.substr(pos, std::string::npos);
2268 slot.n_sent_text += result.text_to_send.size();
2269 // add the token to slot queue and cache
2270 } else {
2271 result.text_to_send = "";
2272 }
2273
2274 slot.add_token(result);
2275 if (slot.params.stream) {
2276 send_partial_response(slot, result);
2277 }
2278 }
2279
2280 if (incomplete) {
2281 slot.has_next_token = true;
2282 }
2283
2284 // if context shifting is disabled, make sure that we don't run out of context
2285 if (!params_base.ctx_shift && slot.n_past + 1 >= slot.n_ctx) {
2286 slot.stop = STOP_TYPE_LIMIT;
2287 slot.has_next_token = false;
2288
2289 SLT_DBG(slot, "stopped due to running out of context, n_past = %d, n_ctx = %d\n", slot.n_past, slot.n_ctx);

Callers

nothing calls this directly

Calls 15

validate_utf8Function · 0.85
minFunction · 0.85
llama_vocab_is_eogFunction · 0.85
llama_model_n_ctx_trainFunction · 0.85
substrMethod · 0.80
findMethod · 0.80
ggml_time_usFunction · 0.50
push_backMethod · 0.45
sizeMethod · 0.45
find_stopping_stringsMethod · 0.45
beginMethod · 0.45
endMethod · 0.45

Tested by

no test coverage detected