Explain how I can modify this repository so that it outputs the logits before and after each sampling.
Assistant
User
Explain how I can extend it to log after each logit, and how to nicely bundle each information to either present it to the user, or to send it via a separate debugging API.
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
To modify the KoboldCpp repository to output logits before and after each sampling, you need to identify where the sampling process happens and inject logging at the appropriate points. Based on the provided source code, the common_sampler_sample function in common/sampling.cpp is the central point where sampling logic is implemented. This function handles the logits setup and subsequent sampling.
Steps to Modify the Code
Here are the steps to log the logits before and after sampling:
1. Locate the Sampling Function
The common_sampler_sample function in common/sampling.cpp is responsible for:
Setting up logits (set_logits function).
Applying samplers (like grammar constraints and sampling chain).
Selecting a token.
This is the ideal place to log the logits.
2. Log the Logits Before Sampling
In the common_sampler_sample function, after calling gsmpl->set_logits(ctx, idx);, you can log the logits before applying any sampling logic.
Add this code block to print the logits:
cpp
void log_logits(const llama_token_data_array & logits) {
printf("Logits before sampling:\n");
for (size_t i = 0; i < logits.size && i < 10; ++i) { // Limit to first 10 logits for readability
printf("Token %d: Logit = %f\n", logits.data[i].id, logits.data[i].logit);
}
}
Then, call the logger after set_logits:
cpp
void common_sampler_sample(struct common_sampler * gsmpl, struct llama_context * ctx, int idx, bool grammar_first) {
gsmpl->set_logits(ctx, idx);
// Log the logits before sampling
log_logits(gsmpl->cur_p);
auto & grmr = gsmpl->grmr;
auto & chain = gsmpl->chain;
auto & cur_p = gsmpl->cur_p; // initialized by set_logits
if (grammar_first) {
llama_sampler_apply(grmr, &cur_p);
}
llama_sampler_apply(chain, &cur_p);
GGML_ASSERT(cur_p.selected != -1 && "no selected token during sampling - check your sampling configuration");
const llama_token id = cur_p.data[cur_p.selected].id;
if (grammar_first) {
return id;
}
// Logits are updated; log them after sampling
printf("Selected token: %d\n", id);
return id;
}
3. Log Logits After Sampling
To log the logits after sampling, you can log the updated logits after applying the grammar or sampling chain. Insert the following at the appropriate location:
cpp
void log_logits_after_sampling(const llama_token_data_array & logits) {
printf("Logits after sampling:\n");
for (size_t i = 0; i < logits.size && i < 10; ++i) { // Limit to first 10 logits for readability
printf("Token %d: Logit = %f, Probability = %f\n", logits.data[i].id, logits.data[i].logit, logits.data[i].p);
}
}
...
llama_token common_sampler_sample(struct common_sampler * gsmpl, struct llama_context * ctx, int idx, bool grammar_first) {
gsmpl->set_logits(ctx, idx);
// Log the logits before sampling
log_logits(gsmpl->cur_p);
auto & grmr = gsmpl->grmr;
auto & chain = gsmpl->chain;
auto & cur_p = gsmpl->cur_p; // initialized by set_logits
if (grammar_first) {
llama_sampler_apply(grmr, &cur_p);
}
llama_sampler_apply(chain, &cur_p);
GGML_ASSERT(cur_p.selected != -1 && "no selected token during sampling - check your sampling configuration");
const llama_token id = cur_p.data[cur_p.selected].id;
// Log logits after sampling
log_logits_after_sampling(gsmpl->cur_p);
if (grammar_first) {
return id;
}
...
}
4. Optional: Log Probabilities
To log probabilities, note that a softmax function might be applied during sampling. If you want to log probabilities explicitly, you need to ensure llama_sampler_softmax_impl is applied and log the p field in llama_token_data.
5. Compile and Test
After applying the changes:
Rebuild the project to ensure your changes are included.
Run a sampling task to verify if the logits are logged as expected.
Additional Notes
Output Control: If you want to control when logits are logged (e.g., toggle logging for debugging), you can implement a global flag or configuration option.
Performance Impact: Logging all logits may slow down the execution, especially for large vocabularies. Consider limiting the number of logits logged (e.g., top 10 logits).
Logit Visualization: If you need a more user-friendly view, consider exporting logits to a file or using a visualization tool like Python's Matplotlib.
By following these steps, you can successfully log the logits before and after each sampling step in the KoboldCpp repository.
To extend the repository to log after each logit and bundle the information nicely for presentation or sending via a debugging API, you need to modify the sampling process to log intermediate steps and create a structured system for managing the logs. Here's how you can achieve this:
Steps to Extend Logging After Each Logit
1. Log After Each Logit
To log after each logit, you need to insert logging after every operation that modifies the logits. This involves:
Logging after applying the grammar constraints.
Logging after applying each sampling method in the sampling chain (e.g., top_k, top_p, temperature).
Logging the final selected token.
Modify the Sampler Apply Functions
The llama_sampler_apply function applies individual samplers (like top_k, top_p, etc.) to the logits. You can inject logging directly into this function.
In llama-sampling.cpp, modify llama_sampler_apply to log logits after each sampler:
cpp
void llama_sampler_apply(struct llama_sampler * smpl, struct llama_token_data_array * cur_p) {
GGML_ASSERT(smpl->iface->apply);
smpl->iface->apply(smpl, cur_p);
// Log logits after applying each sampler
printf("Logits after applying sampler '%s':\n", llama_sampler_name(smpl));
for (size_t i = 0; i < cur_p->size && i < 10; ++i) { // Log top 10 for readability
printf("Token %d: Logit = %f, Probability = %f\n", cur_p->data[i].id, cur_p->data[i].logit, cur_p->data[i].p);
}
}
This ensures that logits are logged after every modification by a sampler in the chain.
Log After Grammar Constraints
In common_sampler_sample, log logits after applying grammar constraints:
cpp
if (grammar_first) {
llama_sampler_apply(grmr, &cur_p);
// Log logits after applying grammar
printf("Logits after grammar constraints:\n");
for (size_t i = 0; i < cur_p.size && i < 10; ++i) {
printf("Token %d: Logit = %f, Probability = %f\n", cur_p.data[i].id, cur_p.data[i].logit, cur_p.data[i].p);
}
}
2. Bundle Information Nicely
To organize and bundle the information, create a structured data format (e.g., a C++ struct) to hold the log information. This makes it easier to present to the user or send via an API.
Define a Log Bundle Struct
Create a struct in sampling.h to hold log data:
cpp
struct LogitLog {
std::string sampler_name; // Name of the sampler applied
std::vector<std::pair<llama_token, float>> logits; // Token IDs and their logits
llama_token selected_token; // Selected token after sampling
std::string stage; // Stage of sampling (e.g., "Grammar", "Top-K", "Final")
};
Populate the LogitLog Struct
Modify llama_sampler_apply to populate the LogitLog struct:
cpp
#include <vector>
#include <string>
void llama_sampler_apply(struct llama_sampler * smpl, struct llama_token_data_array * cur_p) {
GGML_ASSERT(smpl->iface->apply);
smpl->iface->apply(smpl, cur_p);
// Bundle information into LogitLog struct
LogitLog log;
log.sampler_name = llama_sampler_name(smpl);
log.stage = "Sampler Application";
for (size_t i = 0; i < cur_p->size; ++i) {
log.logits.emplace_back(cur_p->data[i].id, cur_p->data[i].logit);
}
// Send log to debugging API or store for later presentation
send_log_to_debug_api(log); // Function to send log (defined later)
}
Add Final Log After Sampling
In common_sampler_sample, log the final selected token:
cpp
// Log final selected token
LogitLog final_log;
final_log.stage = "Final Selection";
final_log.selected_token = id;
for (size_t i = 0; i < cur_p.size; ++i) {
final_log.logits.emplace_back(cur_p.data[i].id, cur_p.data[i].logit);
}
send_log_to_debug_api(final_log); // Send final log
3. Present or Send Logs
Option 1: Present Logs to the User
You can format the logs as a readable string and print them to the console or a file. Create a function to format the LogitLog struct:
cpp
std::string format_logit_log(const LogitLog & log) {
std::string result = "Stage: " + log.stage + "\n";
result += "Sampler: " + log.sampler_name + "\n";
for (const auto & entry : log.logits) {
result += "Token " + std::to_string(entry.first) + ": Logit = " + std::to_string(entry.second) + "\n";
}
if (log.selected_token != -1) {
result += "Selected Token: " + std::to_string(log.selected_token) + "\n";
}
return result;
}
Print the formatted log to the console or write it to a file: