| 837 | } |
| 838 | |
| 839 | std::string Gemma4e::generate(chat_meta_info_t& meta_info, int length_limit, std::ostream& os, std::function<bool()> is_cancelled) { |
| 840 | std::vector<int> sampled_tokens; |
| 841 | std::string result; |
| 842 | if (length_limit > 0){ |
| 843 | sampled_tokens.reserve(length_limit); |
| 844 | } |
| 845 | else{ |
| 846 | sampled_tokens.reserve(4096); |
| 847 | } |
| 848 | assert(this->last_token != -1); |
| 849 | |
| 850 | stop_reason_t reason = EOT_DETECTED; |
| 851 | int last_sampled_token = this->last_token; |
| 852 | |
| 853 | this->token_history.push_back(last_token); |
| 854 | |
| 855 | if (this->is_normal_token(last_sampled_token) && last_sampled_token != -1){ |
| 856 | std::string token_str = this->tokenizer->run_time_decoder(last_sampled_token); |
| 857 | result += token_str; |
| 858 | os << token_str << std::flush; |
| 859 | |
| 860 | } |
| 861 | if (this->is_eos(last_sampled_token)){ |
| 862 | return result; |
| 863 | } |
| 864 | this->profiler_list[DECODING_TIME].reset(); |
| 865 | this->profiler_list[TKOEN_DECODE_TIME].reset(); |
| 866 | if (this->total_tokens >= this->MAX_L){ |
| 867 | header_print("WARNING", "Max length reached, stopping generation..."); |
| 868 | reason = MAX_LENGTH_REACHED; |
| 869 | return result; |
| 870 | } |
| 871 | while (this->total_tokens < this->MAX_L){ |
| 872 | if (is_cancelled()) { |
| 873 | reason = CANCEL_DETECTED; |
| 874 | // reset stream content |
| 875 | buffer_.clear(); |
| 876 | current_mode_ = StreamEventType::CONTENT; |
| 877 | tool_name_.clear(); |
| 878 | is_in_tool_block_ = false; |
| 879 | break; |
| 880 | } |
| 881 | this->profiler_list[DECODING_TIME].start(); |
| 882 | buffer<bf16> y = this->lm_engine->forward(last_sampled_token); |
| 883 | this->profiler_list[DECODING_TIME].stop(1); |
| 884 | |
| 885 | this->profiler_list[SAMPLING_TIME].start(); |
| 886 | int sampled_token = this->sampler->sample(y); |
| 887 | this->profiler_list[SAMPLING_TIME].stop(1); |
| 888 | this->total_tokens++; |
| 889 | last_sampled_token = sampled_token; |
| 890 | |
| 891 | this->profiler_list[TKOEN_DECODE_TIME].start(); |
| 892 | if (this->is_normal_token(sampled_token)){ // filter out special tokens |
| 893 | std::string token_str = this->tokenizer->run_time_decoder(sampled_token); |
| 894 | os << token_str << std::flush; |
| 895 | result += token_str; |
| 896 | } |
nothing calls this directly
no test coverage detected