MCPcopy Create free account
hub / github.com/FastFlowLM/FastFlowLM / generate

Method generate

src/common/AutoModel/modeling_gemma4e.cpp:839–927  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

837}
838
839std::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 }

Callers

nothing calls this directly

Calls 14

cast_to_usFunction · 0.85
push_backMethod · 0.80
is_normal_tokenMethod · 0.80
run_time_decoderMethod · 0.80
is_eosMethod · 0.80
clearMethod · 0.80
forwardMethod · 0.80
sampleMethod · 0.80
get_total_timeMethod · 0.80
reserveMethod · 0.45
resetMethod · 0.45
startMethod · 0.45

Tested by

no test coverage detected