1616#include < executorch/extension/tensor/tensor.h>
1717#include < pytorch/tokenizers/hf_tokenizer.h>
1818#include < rnexecutorch/Error.h>
19- #include < rnexecutorch/Log.h>
2019
2120namespace executorch {
2221namespace extension {
@@ -108,8 +107,6 @@ class TextTokenGenerator {
108107 pos++;
109108 generated_tokens.push_back (cur_token);
110109
111- const bool eos_reached_now = eos_ids_->find (cur_token) != eos_ids_->end ();
112-
113110 if (use_kv_cache_) {
114111 // update the token tensor. token_data will not be empty.
115112 // NOLINTNEXTLINE(facebook-hte-LocalUncheckedArrayBounds)
@@ -121,22 +118,6 @@ class TextTokenGenerator {
121118 tokens_managed, {1 , static_cast <int >(token_data.size ())}));
122119 }
123120
124- // Don't include the terminal EOS/EOT token in the streamed text — it
125- // would otherwise be appended to the assistant message stored in chat
126- // history and corrupt the next turn's chat-template rendering
127- // (e.g. duplicated <end_of_turn>).
128- if (eos_reached_now) {
129- if (!token_cache.empty ()) {
130- auto flush = tokenizer_->decode (token_cache, false );
131- if (flush.ok () && !flush.get ().empty () &&
132- !flush.get ().ends_with (" �" ) && token_callback) {
133- token_callback (flush.get ());
134- }
135- token_cache.clear ();
136- }
137- break ;
138- }
139-
140121 token_cache.push_back (static_cast <uint64_t >(cur_token));
141122
142123 // print the token as string, decode it with the Tokenizer object
@@ -161,7 +142,7 @@ class TextTokenGenerator {
161142 const auto eos_reached = eos_ids_->contains (cur_token);
162143
163144 if (!cache_decoded.ends_with (" �" ) &&
164- (countIntervalElapsed || timeIntervalElapsed || should_stop_)) {
145+ (countIntervalElapsed || timeIntervalElapsed || should_stop_ || eos_reached )) {
165146 token_callback (cache_decoded);
166147 token_cache.clear ();
167148 timestamp_ = std::chrono::high_resolution_clock::now ();
@@ -170,6 +151,12 @@ class TextTokenGenerator {
170151 if (should_stop_) {
171152 break ;
172153 }
154+ // data-dependent terminating condition: we have n_eos_ number of EOS
155+ if (eos_ids_->find (cur_token) != eos_ids_->end ()) {
156+ printf (" \n " );
157+ ET_LOG (Info, " \n Reached to the end of generation" );
158+ break ;
159+ }
173160 }
174161 return pos - start_pos;
175162 }
0 commit comments