-
Notifications
You must be signed in to change notification settings - Fork 14
args, server: Simplify and fix bugs #86
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
danielzgtg
wants to merge
6
commits into
mmwillet:main
Choose a base branch
from
danielzgtg:refactor/args
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
f2ebb39
chore: Enable C++23 and abbreviations
danielzgtg d1399e8
kokoro: Fix double free
danielzgtg c443aae
refactor: Encapsulate args and quantize_impl into examples/
danielzgtg 2d4d444
args, server: Simplify and fix bugs
danielzgtg 1ce486d
Merge branch 'main' into refactor/args
mmwillet d3946b4
Update server.cpp
mmwillet File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,19 @@ | ||
# examples | ||
|
||
include_directories(${CMAKE_CURRENT_SOURCE_DIR}) | ||
add_library(examples_common | ||
args.cpp | ||
args.h | ||
args_common.cpp | ||
args_common.h | ||
audio_file.h | ||
) | ||
target_include_directories(examples_common PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}) | ||
target_link_libraries(examples_common PUBLIC ggml tts) | ||
|
||
if (EMSCRIPTEN) | ||
else() | ||
if (NOT EMSCRIPTEN) | ||
add_subdirectory(cli) | ||
add_subdirectory(perf_battery) | ||
add_subdirectory(quantize) | ||
add_subdirectory(server) | ||
add_subdirectory(phonemize) | ||
endif() | ||
endif () |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
#include "args.h" | ||
|
||
#include <iostream> | ||
#include <sstream> | ||
|
||
void arg::print_help() const { | ||
cout << "--" << full_name; | ||
if (*abbreviation) { | ||
cout << " (-" << abbreviation << ")"; | ||
} | ||
if (*description) { | ||
cout << (required ? ":\n (REQUIRED) " : ":\n (OPTIONAL) ") << description << ".\n"; | ||
} else { | ||
cout << (required ? " is a required parameter.\n" : " is an optional parameter.\n"); | ||
} | ||
} | ||
|
||
void arg::parse(span<str> & argv) { | ||
danielzgtg marked this conversation as resolved.
Show resolved
Hide resolved
|
||
required = false; | ||
if (const auto bool_param{get_if<bool>(&value)}) { | ||
*bool_param = true; | ||
return; | ||
} | ||
if (argv.empty()) { | ||
fprintf(stderr, "The option '--%s' requires an argument\n", full_name); | ||
exit(1); | ||
} | ||
const str a = argv[0]; | ||
argv = argv.subspan(1); | ||
if (const auto string_param{get_if<str>(&value)}) { | ||
*string_param = a; | ||
} else if (const auto int_param{get_if<int>(&value)}) { | ||
istringstream{a} >> *int_param; | ||
} else if (const auto float_param{get_if<float>(&value)}) { | ||
istringstream{a} >> *float_param; | ||
} | ||
} | ||
|
||
void arg_list::parse(int argc, str argv_[]) { | ||
TTS_ASSERT(argc); | ||
span<str> argv{argv_, static_cast<size_t>(argc)}; | ||
argv = argv.subspan(1); | ||
while (!argv.empty()) { | ||
str name{argv[0]}; | ||
if (*name != '-') { | ||
fprintf(stderr, "Only named arguments are supported\n"); | ||
exit(1); | ||
} | ||
++name; | ||
const map<sv, size_t> * lookup = &abbreviations; | ||
if (*name == '-') { | ||
++name; | ||
lookup = &full_names; | ||
if (name == "help"sv) { | ||
for (const size_t i : full_names | views::values) { | ||
args[i].print_help(); | ||
} | ||
exit(0); | ||
} | ||
} | ||
const auto found = lookup->find(sv{name}); | ||
if (found == lookup->end()) { | ||
fprintf(stderr, "argument '%s' is not a valid argument. " | ||
"Call '--help' for information on all valid arguments.\n", argv[0]); | ||
exit(1); | ||
} | ||
argv = argv.subspan(1); | ||
args[found->second].parse(argv); | ||
} | ||
for (const arg & x : args) { | ||
if (x.required) { | ||
fprintf(stderr, "argument '--%s' is required.\n", x.full_name); | ||
exit(1); | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
#pragma once | ||
|
||
#include <map> | ||
#include <thread> | ||
#include <vector> | ||
|
||
#include "imports.h" | ||
|
||
/** | ||
* Holder of one argument. | ||
*/ | ||
class arg { | ||
variant<bool, str, int, float> value; | ||
bool required; | ||
|
||
void print_help() const; | ||
|
||
void parse(span<str> & argv); | ||
|
||
friend class arg_list; | ||
|
||
public: | ||
const str full_name; | ||
const str abbreviation; | ||
const str description; | ||
|
||
template <typename T> | ||
constexpr arg(T default_value, str full_name, str abbreviation, str description, bool required = false) | ||
: value{default_value}, required{required}, | ||
full_name{full_name}, abbreviation{abbreviation}, description{description} { | ||
TTS_ASSERT(full_name[0] != '-'); | ||
TTS_ASSERT(abbreviation[0] != '-'); | ||
} | ||
|
||
template <typename T> | ||
requires is_same_v<T, bool> || is_same_v<T, str> || is_same_v<T, int> || is_same_v<T, float> | ||
// ReSharper disable once CppNonExplicitConversionOperator // We want this to automatically cast | ||
constexpr operator T() const { // NOLINT(*-explicit-constructor) | ||
return get<T>(value); | ||
} | ||
}; | ||
|
||
class arg_list { | ||
vector<arg> args{}; | ||
map<sv, size_t> full_names{}; | ||
map<sv, size_t> abbreviations{}; | ||
|
||
public: | ||
void add(const arg & x) { | ||
const size_t i{args.size()}; | ||
args.push_back(x); | ||
TTS_ASSERT(!full_names.contains(args[i].full_name)); | ||
full_names[args[i].full_name] = i; | ||
if (*args[i].abbreviation) { | ||
abbreviations[args[i].abbreviation] = i; | ||
} | ||
} | ||
|
||
void parse(int argc, str argv_[]); | ||
|
||
constexpr const arg & operator [](sv full_name) const noexcept { | ||
TTS_ASSERT(full_name[0] != '-'); | ||
return args[full_names.at(full_name)]; | ||
} | ||
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
#include "args_common.h" | ||
|
||
#include "tts.h" | ||
|
||
void add_baseline_args(arg_list & args) { | ||
// runner_from_file | ||
args.add({"", "model-path", "mp", "The local path of the gguf model(s) to load", true}); | ||
args.add({ | ||
max(static_cast<int>(thread::hardware_concurrency()), 1), "n-threads", "nt", | ||
"The number of CPU threads to run calculations with. Defaults to known hardware concurrency. " | ||
"If hardware concurrency cannot be determined then it defaults to 1" | ||
}); | ||
} | ||
|
||
static constexpr generation_configuration default_config{}; | ||
|
||
void add_common_args(arg_list & args) { | ||
add_baseline_args(args); | ||
// generation_configuration | ||
args.add({!default_config.use_cross_attn, "no-cross-attn", "ca", "Whether to not include cross attention"}); | ||
args.add({default_config.temperature, "temperature", "t", "The temperature to use when generating outputs"}); | ||
args.add({ | ||
default_config.repetition_penalty, "repetition-penalty", "r", | ||
"The per-channel repetition penalty to be applied the sampled output of the model" | ||
}); | ||
args.add({ | ||
default_config.top_p, "top-p", "mt", | ||
"The sum of probabilities to sample over. Must be a value between 0.0 and 1.0. Defaults to 1.0" | ||
}); | ||
args.add({ | ||
default_config.top_k, "topk", "tk", | ||
"When set to an integer value greater than 0 generation uses nucleus sampling over topk nucleus size. " | ||
"Defaults to 50" | ||
}); | ||
args.add({ | ||
default_config.max_tokens, "max-tokens", "mt", | ||
"The max audio tokens or token batches to generate where each represents approximates 11 ms of audio. " | ||
"Only applied to Dia generation. If set to zero as is its default then the default max generation size. " | ||
"Warning values under 15 are not supported" | ||
}); | ||
args.add({ | ||
default_config.voice, "voice", "v", | ||
"The voice to use to generate the audio. This is only used for models with voice packs" | ||
}); | ||
add_espeak_voice_arg(args); | ||
// runner_from_file | ||
args.add({false, "use-metal", "m", "Whether to use metal acceleration"}); | ||
} | ||
|
||
generation_configuration parse_generation_config(const arg_list & args) { | ||
const generation_configuration config{ | ||
.use_cross_attn{!args["no-cross-attn"]}, | ||
.temperature{args["temperature"]}, | ||
.repetition_penalty{args["repetition-penalty"]}, | ||
.top_p{args["top-p"]}, | ||
.top_k{args["topk"]}, | ||
.max_tokens{args["max-tokens"]}, | ||
.voice{args["voice"]}, | ||
.espeak_voice_id{args["espeak-voice-id"]} | ||
}; | ||
if (config.top_p > 1.0f || config.top_p <= 0.0f) { | ||
fprintf(stderr, "The '--top-p' value must be between 0.0 and 1.0. It was set to '%.6f'.\n", config.top_p); | ||
exit(1); | ||
} | ||
return config; | ||
} | ||
|
||
tts_runner * runner_from_args(const arg_list & args, const generation_configuration & config) { | ||
return runner_from_file(args["model-path"], args["n-threads"], config, !args["use-metal"]); | ||
} | ||
|
||
void add_text_encoder_arg(arg_list & args) { | ||
args.add({ | ||
"", "text-encoder-path", "tep", | ||
"The local path of the text encoder gguf model for conditional generation" | ||
}); | ||
} | ||
|
||
void add_espeak_voice_arg(arg_list & args) { | ||
args.add({ | ||
default_config.espeak_voice_id, "espeak-voice-id", "eid", | ||
"The eSpeak voice id to use for phonemization. " | ||
"This should only be specified when the correct eSpeak voice cannot be inferred from the Kokoro voice. " | ||
"See MultiLanguage Configuration in the README for more info" | ||
}); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
#pragma once | ||
|
||
#include "args.h" | ||
#include "common.h" | ||
|
||
void add_baseline_args(arg_list & args); | ||
void add_common_args(arg_list & args); | ||
|
||
generation_configuration parse_generation_config(const arg_list & args); | ||
tts_runner * runner_from_args(const arg_list & args, const generation_configuration & config); | ||
|
||
void add_text_encoder_arg(arg_list & args); | ||
void add_espeak_voice_arg(arg_list & args); |
File renamed without changes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: it is strange to have the last period added here given that all other punctuation is added by the description.