Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 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.
for (size_t i = 0; i < opts.batch_size; ++i) {
std::vector<int32_t> random_tokens(num_prompt_tokens);
std::generate(random_tokens.begin(), random_tokens.end(), [&]() {
return dist(rng);
});
prompt_sequences->Append(random_tokens.data(), random_tokens.size());
}
} 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
25 changes: 24 additions & 1 deletion benchmark/c/options.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,12 @@ 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"
<< " Note: --prompt_length, --prompt, and --prompt_file are mutually exclusive.\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, --prompt_file, and --use_random_tokens are mutually exclusive;\n"
<< " --use_random_tokens requires --prompt_length.\n"
<< " -g,--generation_length <number>\n"
<< " Number of tokens to generate. Default: " << defaults.num_tokens_to_generate << "\n"
<< " -r,--repetitions <number>\n"
Expand Down Expand Up @@ -95,6 +100,19 @@ void VerifyOptions(const Options& opts) {
throw std::runtime_error("ONNX model directory path must be provided.");
}

const int prompt_source_count = opts.prompt_length_specified + opts.prompt_specified + opts.prompt_file_specified;
if (prompt_source_count > 1) {
throw std::runtime_error("--prompt_length, --prompt, and --prompt_file are mutually exclusive.");
}

if (opts.use_random_tokens && !opts.prompt_length_specified) {
throw std::runtime_error("--use_random_tokens requires -l/--prompt_length.");
}

if (opts.use_random_tokens && (opts.prompt_specified || opts.prompt_file_specified)) {
throw std::runtime_error("--use_random_tokens cannot be used with --prompt or --prompt_file.");
}

// validate execution provider since it has a valid value
ValidateExecutionProvider(opts.execution_provider);
}
Expand Down Expand Up @@ -126,10 +144,13 @@ Options ParseOptionsFromCommandLine(int argc, const char* const* argv) {
opts.batch_size = ParseNumber<size_t>(next_arg(i));
} else if (arg == "-l" || arg == "--prompt_length") {
prompt_num_tokens_or_content = ParseNumber<size_t>(next_arg(i));
opts.prompt_length_specified = true;
} else if (arg == "--prompt") {
prompt_num_tokens_or_content = std::string{next_arg(i)};
opts.prompt_specified = true;
} else if (arg == "--prompt_file") {
prompt_num_tokens_or_content = ReadFileContent(next_arg(i));
opts.prompt_file_specified = true;
} else if (arg == "-g" || arg == "--generation_length") {
opts.num_tokens_to_generate = ParseNumber<size_t>(next_arg(i));
} else if (arg == "-r" || arg == "--repetitions") {
Expand All @@ -140,6 +161,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
4 changes: 4 additions & 0 deletions benchmark/c/options.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,12 @@ struct Options {
size_t num_iterations{5};
size_t num_warmup_iterations{1};
int64_t max_length{0};
bool prompt_length_specified{};
bool prompt_specified{};
bool prompt_file_specified{};
Comment thread
VishalX marked this conversation as resolved.
Outdated
bool verbose{};
bool reuse_generator{};
bool use_random_tokens{};
};

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