| 248 | } |
| 249 | |
| 250 | std::string AutoModel::_shared_generate(chat_meta_info_t& meta_info, int length_limit, std::ostream& os, std::function<bool()> is_cancelled) { |
| 251 | std::vector<int> sampled_tokens; |
| 252 | std::string result; |
| 253 | if (length_limit > 0){ |
| 254 | sampled_tokens.reserve(length_limit); |
| 255 | } |
| 256 | else{ |
| 257 | sampled_tokens.reserve(4096); |
| 258 | } |
| 259 | assert(this->last_token != -1); |
| 260 | |
| 261 | stop_reason_t reason = EOT_DETECTED; |
| 262 | int last_sampled_token = this->last_token; |
| 263 | this->token_history.push_back(this->last_token); |
| 264 | if (this->is_normal_token(last_sampled_token) && last_sampled_token != -1){ |
| 265 | std::string token_str = this->tokenizer->run_time_decoder(last_sampled_token); |
| 266 | result += token_str; |
| 267 | os << token_str << std::flush; |
| 268 | |
| 269 | } |
| 270 | if (this->is_eos(last_sampled_token)){ |
| 271 | return result; |
| 272 | } |
| 273 | this->profiler_list[DECODING_TIME].reset(); |
| 274 | this->profiler_list[TKOEN_DECODE_TIME].reset(); |
| 275 | if (this->total_tokens >= this->MAX_L){ |
| 276 | header_print("WARNING", "Max length reached, stopping generation..."); |
| 277 | reason = MAX_LENGTH_REACHED; |
| 278 | return result; |
| 279 | } |
| 280 | while (this->total_tokens < this->MAX_L){ |
| 281 | if (is_cancelled()) { |
| 282 | reason = CANCEL_DETECTED; |
| 283 | // reset stream content |
| 284 | buffer_.clear(); |
| 285 | current_mode_ = StreamEventType::CONTENT; |
| 286 | tool_name_.clear(); |
| 287 | is_in_tool_block_ = false; |
| 288 | break; |
| 289 | } |
| 290 | this->profiler_list[DECODING_TIME].start(); |
| 291 | buffer<bf16> y = this->lm_engine->forward(last_sampled_token); |
| 292 | this->profiler_list[DECODING_TIME].stop(1); |
| 293 | |
| 294 | this->profiler_list[SAMPLING_TIME].start(); |
| 295 | int sampled_token = this->sampler->sample(y); |
| 296 | this->profiler_list[SAMPLING_TIME].stop(1); |
| 297 | this->total_tokens++; |
| 298 | last_sampled_token = sampled_token; |
| 299 | |
| 300 | this->profiler_list[TKOEN_DECODE_TIME].start(); |
| 301 | if (this->is_normal_token(sampled_token)){ // filter out special tokens |
| 302 | std::string token_str = this->tokenizer->run_time_decoder(sampled_token); |
| 303 | os << token_str << std::flush; |
| 304 | result += token_str; |
| 305 | } |
| 306 | this->profiler_list[TKOEN_DECODE_TIME].stop(1); |
| 307 | this->token_history.push_back(sampled_token); |
no test coverage detected