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