amami-357m-ro / gpu /longform_context_cache.h
cetusian's picture
Initial release
92dc138 verified
Raw History Blame Contribute Delete
1.27 kB
// SPDX-License-Identifier: Apache-2.0
#pragma once
#include <cstddef>
#include <vector>
namespace nemo_speech::tts {
// Retain previous encoder outputs across short windows. A later chunk may need
// more history than the immediately preceding window happened to contain.
inline bool append_longform_text_context(
const std::vector<float>& text_context, int text_len, int current_chunk_len,
int n_embd, int max_history_tokens, std::vector<float>& history) {
if (n_embd <= 0 || max_history_tokens <= 0 || current_chunk_len < 0 ||
text_len < current_chunk_len ||
text_context.size() != static_cast<size_t>(text_len) * n_embd ||
history.size() % n_embd != 0) {
return false;
}
const size_t maximum = static_cast<size_t>(max_history_tokens) * n_embd;
const size_t added = static_cast<size_t>(current_chunk_len) * n_embd;
if (added >= maximum) {
history.assign(text_context.end() - maximum, text_context.end());
} else {
const size_t keep = maximum - added;
if (history.size() > keep) history.erase(history.begin(), history.end() - keep);
history.insert(history.end(), text_context.end() - added, text_context.end());
}
return true;
}
} // namespace nemo_speech::tts