MCPcopy Create free account
hub / github.com/0xShug0/audio.cpp / gpt2_eval

Function gpt2_eval

external/ggml/examples/gpt-2/main-alloc.cpp:674–724  ·  view source on GitHub ↗

evaluate the transformer - model: the model - allocr: ggml_gallocr to use to allocate the compute buffer - n_threads: number of threads to use - n_past: the context size so far - embd_inp: the embeddings of the tokens in the context - embd_w: the predicted logits for the next token

Source from the content-addressed store, hash-verified

672// - embd_w: the predicted logits for the next token
673//
674bool gpt2_eval(
675 const gpt2_model & model,
676 ggml_gallocr_t allocr,
677 const int n_threads,
678 const int n_past,
679 const std::vector<gpt_vocab::id> & embd_inp,
680 std::vector<float> & embd_w) {
681 const int N = embd_inp.size();
682
683 const auto & hparams = model.hparams;
684
685 const int n_vocab = hparams.n_vocab;
686
687 struct ggml_cgraph * gf = gpt2_graph(model, n_past, embd_inp.size());
688
689 // allocate the graph tensors
690 ggml_gallocr_alloc_graph(allocr, gf);
691
692 // set the graph inputs
693 struct ggml_tensor * embd = ggml_graph_get_tensor(gf, "embd");
694 memcpy(embd->data, embd_inp.data(), N*ggml_element_size(embd));
695
696 struct ggml_tensor * position = ggml_graph_get_tensor(gf, "position");
697 for (int i = 0; i < N; ++i) {
698 ((int32_t *) position->data)[i] = n_past + i;
699 }
700
701 // run the computation
702 struct ggml_cplan plan = ggml_graph_plan(gf, n_threads, nullptr);
703 static std::vector<uint8_t> work_buffer;
704 work_buffer.resize(plan.work_size);
705 plan.work_data = work_buffer.data();
706 ggml_graph_compute(gf, &plan);
707
708 //if (n_past%100 == 0) {
709 // ggml_graph_print (&gf);
710 // ggml_graph_dump_dot(&gf, NULL, "gpt-2.dot");
711 //}
712
713 // get the graph outputs
714 struct ggml_tensor * logits = ggml_graph_get_tensor(gf, "logits");
715
716 //embd_w.resize(n_vocab*N);
717 //memcpy(embd_w.data(), ggml_get_data(logits), sizeof(float)*n_vocab*N);
718
719 // return result just for the last token
720 embd_w.resize(n_vocab);
721 memcpy(embd_w.data(), (float *) ggml_get_data(logits) + (n_vocab*(N-1)), sizeof(float)*n_vocab);
722
723 return true;
724}
725
726int main(int argc, char ** argv) {
727 ggml_time_init();

Callers 1

mainFunction · 0.70

Calls 10

ggml_gallocr_alloc_graphFunction · 0.85
ggml_graph_get_tensorFunction · 0.85
ggml_element_sizeFunction · 0.85
ggml_graph_planFunction · 0.85
ggml_graph_computeFunction · 0.85
ggml_get_dataFunction · 0.85
gpt2_graphFunction · 0.70
sizeMethod · 0.45
dataMethod · 0.45
resizeMethod · 0.45

Tested by

no test coverage detected