| 105 | } |
| 106 | |
| 107 | static void process_logits( |
| 108 | int n_vocab, const float * logits, const int * tokens, int n_token, std::vector<std::thread> & workers, |
| 109 | double & nll, double & nll2, float * logit_history, float * prob_history |
| 110 | ) { |
| 111 | std::mutex mutex; |
| 112 | int counter = 0; |
| 113 | auto compute = [&mutex, &counter, &nll, &nll2, logit_history, prob_history, n_vocab, logits, tokens, n_token] () { |
| 114 | double local_nll = 0; |
| 115 | double local_nll2 = 0; |
| 116 | while (true) { |
| 117 | std::unique_lock<std::mutex> lock(mutex); |
| 118 | int i = counter++; |
| 119 | if (i >= n_token) { |
| 120 | nll += local_nll; nll2 += local_nll2; |
| 121 | break; |
| 122 | } |
| 123 | lock.unlock(); |
| 124 | const results_log_softmax results = log_softmax(n_vocab, logits + size_t(i)*n_vocab, tokens[i+1]); |
| 125 | const double v = -results.log_softmax; |
| 126 | local_nll += v; |
| 127 | local_nll2 += v*v; |
| 128 | |
| 129 | logit_history[i] = results.logit; |
| 130 | prob_history[i] = results.prob; |
| 131 | } |
| 132 | }; |
| 133 | for (auto & w : workers) { |
| 134 | w = std::thread(compute); |
| 135 | } |
| 136 | compute(); |
| 137 | for (auto & w : workers) { |
| 138 | w.join(); |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | static void process_logits(std::ostream& out, int n_vocab, const float * logits, const int * tokens, int n_token, |
| 143 | std::vector<std::thread> & workers, std::vector<uint16_t> & log_probs, double & nll, double & nll2) { |