| 103 | } |
| 104 | |
| 105 | void ngram_rows(const int32_t* tokens, const int32_t* prev, int n_tokens, const PleConsts& c, uint32_t* out) { |
| 106 | const int n_prev = NGRAM_SIZE - 1; |
| 107 | for (int i = 0; i < n_tokens; ++i) { |
| 108 | int64_t ctx[NGRAM_SIZE]; |
| 109 | ctx[0] = tokens[i]; |
| 110 | bool cut = false; |
| 111 | for (int s = 1; s < NGRAM_SIZE; ++s) { |
| 112 | // `prev` is OLDEST FIRST, so predecessor `s` positions back is entry (n_prev - s): s=1 reads the |
| 113 | // NEWEST. Reading index (s-1) instead walks the window backwards, which still produces indices |
| 114 | // in range and so cannot be caught by a range check - only by an oracle. |
| 115 | const int32_t t = cut ? TOKEN_NULL : prev[i * n_prev + (n_prev - s)]; |
| 116 | // The cut is evaluated BEFORE the value is stored, so the position whose predecessor was EOS is |
| 117 | // itself EOS. Storing first and then cutting would leave position s holding the real token while |
| 118 | // position s+1 became EOS - one token of history too much. |
| 119 | cut = cut || t < 0 || t == PLE_EOS_TOKEN_ID; |
| 120 | ctx[s] = cut ? PLE_EOS_TOKEN_ID : t; |
| 121 | } |
| 122 | for (int n = 2; n <= NGRAM_SIZE; ++n) { |
| 123 | const uint64_t mixed = ngram_mixed(ctx, c.mult, n); |
| 124 | const int base = (n - 2) * HEADS_PER_NGRAM; |
| 125 | for (int g = 0; g < HEADS_PER_NGRAM; ++g) { |
| 126 | const int h = base + g; |
| 127 | out[i * PLE_N_HEADS + h] = (uint32_t) (mixed % c.vocab[h] + c.offset[h]); |
| 128 | } |
| 129 | } |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | namespace { |
| 134 | const int8_t kIq4Nl[16] = {-127, -104, -83, -65, -49, -35, -22, -10, 1, 13, 25, 38, 53, 69, 89, 113}; |