Lucasgitguy-co's picture
Turbo-Kev Dictate release
20149a3 verified
Raw History Blame Contribute Delete
4.9 kB
// tk_embd: dump what llama.cpp feeds the LLM for one audio prompt (for the torch <-> llama.cpp parity gate).
// tk_embd <text.gguf> <mmproj.gguf> <pcm_f32le_16k.raw> <prompt.txt> <out_prefix>
// Writes <out_prefix>.json (chunk list: text tokens, audio token count) and <out_prefix>.audio.f32
// (audio embeddings, n_tokens x n_embd, row major). Prompt is the fully rendered chat text with the media marker.
// Optional 6th arg n_gen: also decode greedily and add "gen": [token ids] and "margin": [top1-top2 logit] to the json.
#include "llama.h"
#include "mtmd.h"
#include "mtmd-helper.h"
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
int main(int argc, char ** argv) {
if (argc != 6 && argc != 7) { fprintf(stderr, "usage: tk_embd text.gguf mmproj.gguf pcm.raw prompt.txt out_prefix\n"); return 2; }
llama_backend_init();
llama_model_params mp = llama_model_default_params();
llama_model * model = llama_model_load_from_file(argv[1], mp);
if (!model) { fprintf(stderr, "model load failed\n"); return 1; }
mtmd_context_params cp = mtmd_context_params_default();
cp.use_gpu = getenv("TK_CPU") == nullptr; cp.print_timings = false; cp.warmup = false;
mtmd_context * ctx = mtmd_init_from_file(argv[2], model, cp);
if (!ctx) { fprintf(stderr, "mmproj load failed\n"); return 1; }
std::ifstream pf(argv[3], std::ios::binary);
std::vector<char> raw((std::istreambuf_iterator<char>(pf)), {});
std::vector<float> pcm(raw.size() / 4);
memcpy(pcm.data(), raw.data(), pcm.size() * 4);
mtmd_bitmap * bmp = mtmd_bitmap_init_from_audio(pcm.size(), pcm.data());
std::ifstream tf(argv[4]); std::stringstream ss; ss << tf.rdbuf(); std::string prompt = ss.str();
mtmd_input_text txt{prompt.c_str(), prompt.size(), /*add_special*/ true, /*parse_special*/ true};
mtmd_input_chunks * chunks = mtmd_input_chunks_init();
const mtmd_bitmap * bmps[1] = {bmp};
int32_t rc = mtmd_tokenize(ctx, chunks, &txt, bmps, 1);
if (rc != 0) { fprintf(stderr, "tokenize rc=%d\n", rc); return 1; }
const int n_embd = llama_model_n_embd_inp(model);
std::string out = argv[5];
FILE * fa = fopen((out + ".audio.f32").c_str(), "wb");
std::ostringstream js; js << "{\"n_embd\": " << n_embd << ", \"chunks\": [";
for (size_t i = 0; i < mtmd_input_chunks_size(chunks); i++) {
const mtmd_input_chunk * c = mtmd_input_chunks_get(chunks, i);
if (i) js << ", ";
if (mtmd_input_chunk_get_type(c) == MTMD_INPUT_CHUNK_TYPE_TEXT) {
size_t n = 0; const llama_token * t = mtmd_input_chunk_get_tokens_text(c, &n);
js << "{\"type\": \"text\", \"tokens\": [";
for (size_t j = 0; j < n; j++) js << (j ? ", " : "") << t[j];
js << "]}";
} else {
if (mtmd_encode_chunk(ctx, c) != 0) { fprintf(stderr, "encode failed\n"); return 1; }
size_t n = mtmd_input_chunk_get_n_tokens(c);
fwrite(mtmd_get_output_embd(ctx), sizeof(float), n * (size_t) n_embd, fa);
js << "{\"type\": \"audio\", \"n_tokens\": " << n << ", \"n_pos\": " << mtmd_input_chunk_get_n_pos(c) << "}";
}
}
js << "], \"use_mrope\": " << (mtmd_decode_use_mrope(ctx) ? "true" : "false");
int n_gen = argc == 7 ? atoi(argv[6]) : 0;
if (n_gen > 0) {
llama_context_params lp = llama_context_default_params();
lp.n_ctx = 4096; lp.n_batch = 2048; lp.n_ubatch = 2048;
llama_context * lctx = llama_init_from_model(model, lp);
llama_pos n_past = 0;
if (mtmd_helper_eval_chunks(ctx, lctx, chunks, 0, 0, 2048, true, &n_past) != 0) { fprintf(stderr, "eval failed\n"); return 1; }
const llama_vocab * vocab = llama_model_get_vocab(model);
const int n_vocab = llama_vocab_n_tokens(vocab);
js << ", \"gen\": [";
std::ostringstream mg; mg << "[";
for (int i = 0; i < n_gen; i++) {
const float * lg = llama_get_logits_ith(lctx, -1);
int b1 = 0, b2 = 1; if (lg[b2] > lg[b1]) std::swap(b1, b2);
for (int k = 2; k < n_vocab; k++) { if (lg[k] > lg[b1]) { b2 = b1; b1 = k; } else if (lg[k] > lg[b2]) b2 = k; }
js << (i ? ", " : "") << b1; mg << (i ? ", " : "") << (lg[b1] - lg[b2]);
if (llama_vocab_is_eog(vocab, b1)) break;
llama_token t = b1;
llama_batch bt = llama_batch_get_one(&t, 1);
if (llama_decode(lctx, bt) != 0) { fprintf(stderr, "decode failed\n"); return 1; }
}
mg << "]";
js << "], \"margin\": " << mg.str();
llama_free(lctx);
}
js << "}";
fclose(fa);
std::ofstream(out + ".json") << js.str();
printf("%s\n", js.str().c_str());
mtmd_input_chunks_free(chunks); mtmd_bitmap_free(bmp); mtmd_free(ctx); llama_model_free(model);
return 0;
}