Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/copilot-instructions.md
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ The repo supports specific model architectures - check `src/models/model_type.h`

### Performance Considerations

- **KV caching** is automatically managed but can be configured via `runtime_settings.cpp`
- **KV caching** is automatically managed but can be configured via `src/runtime_settings.cpp`
- **Continuous decoding** (session continuation) requires careful state management
- **Multi-LoRA** adapters use separate weight loading in `src/models/adapters.cpp`

Expand Down
32 changes: 28 additions & 4 deletions benchmark/c/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@
#include <cmath>
#include <algorithm>
#include <chrono>
#include <cstdint>
#include <random>
#include <iostream>
#include <numeric>
#include <stdexcept>
#include <string>
#include <string_view>
#include <variant>
#include <vector>

#include "ort_genai.h"
Expand Down Expand Up @@ -158,7 +161,10 @@ void RunBenchmark(const benchmark::Options& opts) {
bool need_generate_prompt = false;
std::string prompt;

if (const size_t* npt = std::get_if<size_t>(&opts.prompt_num_tokens_or_content)) {
if (opts.use_random_tokens) {
num_prompt_tokens = std::get<size_t>(opts.prompt_num_tokens_or_content);
need_generate_prompt = false;
} else if (const size_t* npt = std::get_if<size_t>(&opts.prompt_num_tokens_or_content)) {
num_prompt_tokens = *npt;
need_generate_prompt = true;
} else {
Expand Down Expand Up @@ -203,8 +209,21 @@ void RunBenchmark(const benchmark::Options& opts) {
}

auto prompt_sequences = OgaSequences::Create();
for (size_t i = 0; i < opts.batch_size; ++i) {
tokenizer->Encode(prompt.c_str(), *prompt_sequences);
if (opts.use_random_tokens) {
std::random_device rd;
std::mt19937 rng(rd());
std::uniform_int_distribution<int32_t> dist(0, 99);
Comment thread
baijumeswani marked this conversation as resolved.
std::vector<int32_t> row(num_prompt_tokens);
for (size_t i = 0; i < opts.batch_size; ++i) {
for (size_t j = 0; j < num_prompt_tokens; ++j) {
row[j] = dist(rng);
}
prompt_sequences->Append(row.data(), row.size());
}
Comment thread
VishalX marked this conversation as resolved.
Outdated
} else {
for (size_t i = 0; i < opts.batch_size; ++i) {
tokenizer->Encode(prompt.c_str(), *prompt_sequences);
}
}

// warmup
Expand All @@ -226,7 +245,12 @@ void RunBenchmark(const benchmark::Options& opts) {

if (opts.verbose && i == 0) {
// show prompt and output on first iteration
std::cout << "[PROMPT BEGIN]" << prompt << "[PROMPT END]\n";
if (opts.use_random_tokens) {
std::cout << "[PROMPT] random token IDs in [0, 99], batch_size=" << opts.batch_size
<< ", tokens per sequence=" << num_prompt_tokens << "\n";
} else {
std::cout << "[PROMPT BEGIN]" << prompt << "[PROMPT END]\n";
}
const auto output_sequence_length = gen->TokenCount();
const auto* output_sequence_data = gen->GetSequenceData(0);
const auto output = tokenizer->Decode(output_sequence_data, output_sequence_length);
Expand Down
11 changes: 11 additions & 0 deletions benchmark/c/options.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ namespace {
<< " Prompt text to use. Default: See --prompt_length.\n"
<< " --prompt_file <file containing prompt text>\n"
<< " Path to file containing prompt text to use. Default: See --prompt_length.\n"
<< " --use_random_tokens\n"
<< " Use random token IDs in [0, 99] per position instead of generating or encoding\n"
<< " a text prompt. Requires -l/--prompt_length (cannot be used with --prompt or\n"
<< " --prompt_file).\n"
<< " Note: --prompt_length, --prompt, and --prompt_file are mutually exclusive.\n"
Comment thread
VishalX marked this conversation as resolved.
Outdated
<< " -g,--generation_length <number>\n"
<< " Number of tokens to generate. Default: " << defaults.num_tokens_to_generate << "\n"
Expand Down Expand Up @@ -95,6 +99,11 @@ void VerifyOptions(const Options& opts) {
throw std::runtime_error("ONNX model directory path must be provided.");
}

if (opts.use_random_tokens && !std::holds_alternative<size_t>(opts.prompt_num_tokens_or_content)) {
throw std::runtime_error(
"--use_random_tokens requires -l/--prompt_length and cannot be used with --prompt or --prompt_file.");
}
Comment thread
VishalX marked this conversation as resolved.
Outdated

// validate execution provider since it has a valid value
ValidateExecutionProvider(opts.execution_provider);
}
Expand Down Expand Up @@ -140,6 +149,8 @@ Options ParseOptionsFromCommandLine(int argc, const char* const* argv) {
opts.max_length = ParseNumber<int64_t>(next_arg(i));
} else if (arg == "--reuse_generator") {
opts.reuse_generator = true;
} else if (arg == "--use_random_tokens") {
opts.use_random_tokens = true;
} else if (arg == "-v" || arg == "--verbose") {
opts.verbose = true;
} else if (arg == "-h" || arg == "--help") {
Expand Down
1 change: 1 addition & 0 deletions benchmark/c/options.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ struct Options {
int64_t max_length{0};
bool verbose{};
bool reuse_generator{};
bool use_random_tokens{};
};

Options ParseOptionsFromCommandLine(int argc, const char* const* argv);
Expand Down
Loading