Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get logits as numpy array #4

Merged
merged 3 commits into from
Apr 28, 2023
Merged

Get logits as numpy array #4

merged 3 commits into from
Apr 28, 2023

Conversation

KeremP
Copy link
Contributor

@KeremP KeremP commented Apr 28, 2023

I believe this could address nomic-ai/pygpt4all#3.

logits are passed to a callback function at each iteration as a numpy array.

for (int i = embd.size(); i < embd_inp.size() + params.n_predict; i++) {
        // predict
        if (embd.size() > 0) {
            const int64_t t_start_us = ggml_time_us();

            if (!gptj_eval(model, params.n_threads, n_past, embd, logits, mem_per_token)) {
                printf("Failed to predict\n");
                return 1;
            }
            // collect logits for each token
            py::array_t<float> _logits = py::array_t<float>{model.hparams.n_vocab, logits.data(), py::none()};
            logits_callback(_logits);
            t_predict_us += ggml_time_us() - t_start_us;
        }
...
}
def _call_logits_callback(self, logits: np.ndarray):
        """
        Internal logits_callback that saves the logit representation at each token.
        :return: None
        """
        self.logits.append(logits.tolist())
        
        if Model._logits_callback is not None:
            Model._logits_callback(logits)

Vectors are appended to self.logits and can be dumped to .npy by calling model.braindump("output_path")

@absadiki
Copy link
Owner

That's great @KeremP.
Thank you very much.

@absadiki absadiki merged commit e04bf59 into absadiki:main Apr 28, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants