| 106 | |
| 107 | |
| 108 | std::string Nanbeige::generate(chat_meta_info_t& meta_info, int length_limit, std::ostream& os, std::function<bool()> is_cancelled) { |
| 109 | //header_print("is_cancelled", is_cancelled); |
| 110 | std::vector<int> sampled_tokens; |
| 111 | std::string result; |
| 112 | if (length_limit > 0){ |
| 113 | sampled_tokens.reserve(length_limit); |
| 114 | } |
| 115 | else{ |
| 116 | sampled_tokens.reserve(4096); |
| 117 | } |
| 118 | assert(this->last_token != -1); |
| 119 | |
| 120 | stop_reason_t reason = EOT_DETECTED; |
| 121 | int last_sampled_token = this->last_token; |
| 122 | this->token_history.push_back(this->last_token); |
| 123 | if (this->is_normal_token(last_sampled_token) && last_sampled_token != -1){ |
| 124 | std::string token_str = this->nanbeige_filter(last_sampled_token); |
| 125 | result += token_str; |
| 126 | os << token_str << std::flush; |
| 127 | |
| 128 | } |
| 129 | if (this->is_eos(last_sampled_token)){ |
| 130 | return result; |
| 131 | } |
| 132 | this->profiler_list[DECODING_TIME].reset(); |
| 133 | this->profiler_list[TKOEN_DECODE_TIME].reset(); |
| 134 | if (this->total_tokens >= this->MAX_L){ |
| 135 | header_print("WARNING", "Max length reached, stopping generation..."); |
| 136 | reason = MAX_LENGTH_REACHED; |
| 137 | return result; |
| 138 | } |
| 139 | while (this->total_tokens < this->MAX_L){ |
| 140 | if (is_cancelled()) { |
| 141 | reason = CANCEL_DETECTED; |
| 142 | // reset stream content |
| 143 | buffer_.clear(); |
| 144 | current_mode_ = StreamEventType::CONTENT; |
| 145 | tool_name_.clear(); |
| 146 | is_in_tool_block_ = false; |
| 147 | break; |
| 148 | } |
| 149 | this->profiler_list[DECODING_TIME].start(); |
| 150 | buffer<bf16> y = this->lm_engine->forward(last_sampled_token); |
| 151 | this->profiler_list[DECODING_TIME].stop(1); |
| 152 | |
| 153 | this->profiler_list[SAMPLING_TIME].start(); |
| 154 | int sampled_token = this->sampler->sample(y); |
| 155 | this->profiler_list[SAMPLING_TIME].stop(1); |
| 156 | this->total_tokens++; |
| 157 | last_sampled_token = sampled_token; |
| 158 | |
| 159 | this->profiler_list[TKOEN_DECODE_TIME].start(); |
| 160 | if (this->is_normal_token(sampled_token)){ // filter out special tokens |
| 161 | std::string token_str = this->nanbeige_filter(sampled_token); |
| 162 | os << token_str << std::flush; |
| 163 | result += token_str; |
| 164 | } |
| 165 | this->profiler_list[TKOEN_DECODE_TIME].stop(1); |
nothing calls this directly
no test coverage detected