MCPcopy Create free account
hub / github.com/antirez/ds4 / dist_write_logprobs_dump

Function dist_write_logprobs_dump

ds4_distributed.c:2989–3090  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

2987}
2988
2989static int dist_write_logprobs_dump(
2990 ds4_dist_coordinator_state *state,
2991 const ds4_dist_generation_options *gen,
2992 const ds4_tokens *prompt,
2993 ds4_dist_route_plan *plan,
2994 ds4_session *session,
2995 uint64_t session_id,
2996 uint64_t *request_id,
2997 float *logits) {
2998 FILE *fp = fopen(gen->dump_logprobs_path, "wb");
2999 if (!fp) {
3000 fprintf(stderr, "ds4: failed to open distributed --dump-logprobs file: %s\n",
3001 gen->dump_logprobs_path);
3002 return 1;
3003 }
3004
3005 int k = gen->dump_logprobs_top_k > 0 ? gen->dump_logprobs_top_k : 20;
3006 if (k > 128) k = 128;
3007 ds4_dist_logprob *scores = calloc((size_t)k, sizeof(scores[0]));
3008 if (!scores) {
3009 fclose(fp);
3010 return 1;
3011 }
3012
3013 ds4_tokens transcript = {0};
3014 ds4_tokens_copy(&transcript, prompt);
3015
3016 int max_tokens = gen->n_predict;
3017 int room = gen->ctx_size - prompt->len;
3018 if (room <= 1) max_tokens = 0;
3019 else if (max_tokens > room - 1) max_tokens = room - 1;
3020
3021 fprintf(fp,
3022 "{\n"
3023 " \"source\":\"ds4-distributed\",\n"
3024 " \"prompt_tokens\":%d,\n"
3025 " \"ctx\":%d,\n"
3026 " \"top_k\":%d,\n"
3027 " \"route_count\":%u,\n"
3028 " \"steps\":[\n",
3029 prompt->len,
3030 gen->ctx_size,
3031 k,
3032 plan->count);
3033
3034 char err[256];
3035 int rc = 0;
3036 const int eos = ds4_token_eos(state->engine);
3037 for (int generated = 0; generated < max_tokens; generated++) {
3038 const int n = dist_logits_top_logprobs(logits, ds4_engine_vocab_size(state->engine), scores, k);
3039 const int token = dist_logits_argmax(logits, ds4_engine_vocab_size(state->engine));
3040 if (generated) fputs(",\n", fp);
3041 fprintf(fp, " {\"step\":%d,\"selected\":", generated);
3042 dist_json_write_token(fp, state->engine, token);
3043 fputs(",\"top_logprobs\":[", fp);
3044 for (int i = 0; i < n; i++) {
3045 if (i) fputc(',', fp);
3046 fputs("{\"token\":", fp);

Callers 1

Calls 10

ds4_tokens_copyFunction · 0.85
ds4_token_eosFunction · 0.85
dist_logits_top_logprobsFunction · 0.85
ds4_engine_vocab_sizeFunction · 0.85
dist_logits_argmaxFunction · 0.85
dist_json_write_tokenFunction · 0.85
ds4_tokens_pushFunction · 0.85
ds4_tokens_freeFunction · 0.85

Tested by

no test coverage detected