File size: 1,269 Bytes
92dc138
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
// 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