| 41 | } |
| 42 | |
| 43 | std::vector<int> MtokTokenizer::encodeSingle(const std::string& sentence, int maxlen) const { |
| 44 | #if defined(MNN_DIFFUSION_WITH_LLM_TOKENIZER) |
| 45 | if (mTokenizer == nullptr) { |
| 46 | return {}; |
| 47 | } |
| 48 | std::vector<int> ids = mTokenizer->encode(sentence); |
| 49 | if (mBosId >= 0 && (ids.empty() || ids.front() != mBosId)) { |
| 50 | ids.insert(ids.begin(), mBosId); |
| 51 | } |
| 52 | if (mEosId >= 0 && (ids.empty() || ids.back() != mEosId)) { |
| 53 | ids.push_back(mEosId); |
| 54 | } |
| 55 | if (maxlen > 0) { |
| 56 | if ((int)ids.size() > maxlen) { |
| 57 | ids.resize(maxlen); |
| 58 | if (mEosId >= 0) { |
| 59 | ids[maxlen - 1] = mEosId; |
| 60 | } |
| 61 | } else { |
| 62 | while ((int)ids.size() < maxlen) { |
| 63 | ids.push_back(0); |
| 64 | } |
| 65 | } |
| 66 | } |
| 67 | return ids; |
| 68 | #else |
| 69 | (void)sentence; |
| 70 | (void)maxlen; |
| 71 | return {}; |
| 72 | #endif |
| 73 | } |
| 74 | |
| 75 | std::vector<int> MtokTokenizer::encode(const std::string& sentence, int maxlen) { |
| 76 | #if defined(MNN_DIFFUSION_WITH_LLM_TOKENIZER) |