Reworked to be self-contained and more universal
This commit is contained in:
parent
d8350a6a1b
commit
faefd46f99
4 changed files with 128 additions and 90 deletions
|
@ -1,5 +1,6 @@
|
||||||
#include "common.h"
|
#include "common.h"
|
||||||
#include "llama.h"
|
#include "llama.h"
|
||||||
|
#include "json-util.hpp"
|
||||||
|
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
#include <cassert>
|
#include <cassert>
|
||||||
|
@ -115,85 +116,6 @@ void process_escapes(std::string& input) {
|
||||||
input.resize(output_idx);
|
input.resize(output_idx);
|
||||||
}
|
}
|
||||||
|
|
||||||
nlohmann::json get_json(const char* file_name) noexcept {
|
|
||||||
try {
|
|
||||||
printf("Opening a json file %s\n", file_name);
|
|
||||||
std::ifstream jstream(file_name);
|
|
||||||
return nlohmann::json::parse(jstream);
|
|
||||||
}
|
|
||||||
catch (const std::exception& ex) {
|
|
||||||
fprintf(stderr, "%s\n", ex.what());
|
|
||||||
}
|
|
||||||
|
|
||||||
return {};
|
|
||||||
}
|
|
||||||
|
|
||||||
// standalone parsing attempt
|
|
||||||
std::vector<std::string> args_parse_json_only_string(char* file_name) {
|
|
||||||
std::vector<std::string> arguments_w_json;
|
|
||||||
nlohmann::json file_config = get_json(file_name);
|
|
||||||
if (!file_config.empty()) { // ensures no unnecessary work
|
|
||||||
arguments_w_json.push_back(file_name);
|
|
||||||
|
|
||||||
for (auto& p : file_config.items()) {
|
|
||||||
// only use strings, numbers and booleans for switches
|
|
||||||
if (p.value().is_string() || p.value().is_number() || p.value().is_boolean()) {
|
|
||||||
arguments_w_json.push_back(p.key());
|
|
||||||
|
|
||||||
if (!p.value().is_boolean()) {
|
|
||||||
std::string param_value;
|
|
||||||
if (p.value().is_string()) {
|
|
||||||
param_value = p.value().get<std::string>();
|
|
||||||
} else if (p.value().is_number()) {
|
|
||||||
param_value = std::to_string(p.value().get<float>()); // works for int values too
|
|
||||||
}
|
|
||||||
arguments_w_json.push_back(param_value);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return arguments_w_json;
|
|
||||||
}
|
|
||||||
|
|
||||||
// this variant seems safer, we can clear args after processing
|
|
||||||
bool gpt_params_parse_json(char* file_name, gpt_params & params) {
|
|
||||||
bool result = true;
|
|
||||||
std::vector<std::string> arguments = args_parse_json_only_string(file_name);
|
|
||||||
|
|
||||||
if (!arguments.empty()) { // ensures no unnecessary work
|
|
||||||
int argc_json = arguments.size();
|
|
||||||
char** args_json = new char*[arguments.size()];
|
|
||||||
for(size_t i = 0; i < arguments.size(); i++) {
|
|
||||||
args_json[i] = new char[arguments[i].size() + 1];
|
|
||||||
strcpy(args_json[i], arguments[i].c_str());
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
|
||||||
if (!gpt_params_parse_ex(argc_json, args_json, params)) {
|
|
||||||
gpt_print_usage(argc_json, args_json, gpt_params());
|
|
||||||
exit(0);
|
|
||||||
}
|
|
||||||
for (size_t i = 0; i < arguments.size(); i++) {
|
|
||||||
delete [] args_json[i];
|
|
||||||
}
|
|
||||||
delete [] args_json;
|
|
||||||
}
|
|
||||||
catch (const std::invalid_argument & ex) {
|
|
||||||
fprintf(stderr, "%s\n", ex.what());
|
|
||||||
gpt_print_usage(argc_json, args_json, gpt_params());
|
|
||||||
exit(1);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
// let's also print help, pointing at a faulty file name/parameter
|
|
||||||
char** args = new char* {file_name};
|
|
||||||
gpt_print_usage(1, args, gpt_params());
|
|
||||||
exit(1);
|
|
||||||
}
|
|
||||||
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool gpt_params_parse(int argc, char ** argv, gpt_params & params) {
|
bool gpt_params_parse(int argc, char ** argv, gpt_params & params) {
|
||||||
bool result = true;
|
bool result = true;
|
||||||
|
|
||||||
|
@ -836,8 +758,14 @@ bool gpt_params_parse_ex(int argc, char ** argv, gpt_params & params) {
|
||||||
// End of Parse args for logging parameters
|
// End of Parse args for logging parameters
|
||||||
#endif // LOG_DISABLE_LOGS
|
#endif // LOG_DISABLE_LOGS
|
||||||
} else {
|
} else {
|
||||||
if (!gpt_params_parse_json(argv[i], params)) { // attempt to read as a file
|
args_struct args_obj(argv[i]);
|
||||||
invalid_param = true;
|
|
||||||
|
if (args_obj.argc > 0) {
|
||||||
|
int argc_json = args_obj.argc;
|
||||||
|
char** args_json = args_obj.argv;
|
||||||
|
|
||||||
|
return gpt_params_parse(argc_json, args_json, params);
|
||||||
|
} else {
|
||||||
throw std::invalid_argument("error: unknown argument: " + arg);
|
throw std::invalid_argument("error: unknown argument: " + arg);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,8 +6,6 @@
|
||||||
|
|
||||||
#include "sampling.h"
|
#include "sampling.h"
|
||||||
|
|
||||||
#include "examples/server/json.hpp"
|
|
||||||
|
|
||||||
#define LOG_NO_FILE_LINE_FUNCTION
|
#define LOG_NO_FILE_LINE_FUNCTION
|
||||||
#include "log.h"
|
#include "log.h"
|
||||||
|
|
||||||
|
@ -136,12 +134,6 @@ struct gpt_params {
|
||||||
std::string image = ""; // path to an image file
|
std::string image = ""; // path to an image file
|
||||||
};
|
};
|
||||||
|
|
||||||
nlohmann::json get_json(const char* file_name) noexcept;
|
|
||||||
|
|
||||||
std::vector<std::string> args_parse_json_only_string(char* file_name);
|
|
||||||
|
|
||||||
bool gpt_params_parse_json(char* file_name, gpt_params & params);
|
|
||||||
|
|
||||||
bool gpt_params_parse_ex(int argc, char ** argv, gpt_params & params);
|
bool gpt_params_parse_ex(int argc, char ** argv, gpt_params & params);
|
||||||
|
|
||||||
bool gpt_params_parse(int argc, char ** argv, gpt_params & params);
|
bool gpt_params_parse(int argc, char ** argv, gpt_params & params);
|
||||||
|
|
110
common/json-util.hpp
Normal file
110
common/json-util.hpp
Normal file
|
@ -0,0 +1,110 @@
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
#include <vector>
|
||||||
|
#include <fstream>
|
||||||
|
#include "examples/server/json.hpp"
|
||||||
|
|
||||||
|
static nlohmann::json get_json(const char* file_name) noexcept {
|
||||||
|
try {
|
||||||
|
printf("Opening a json file %s\n", file_name);
|
||||||
|
std::ifstream jstream(file_name);
|
||||||
|
return nlohmann::json::parse(jstream);
|
||||||
|
}
|
||||||
|
catch (const std::exception& ex) {
|
||||||
|
fprintf(stderr, "%s\n", ex.what());
|
||||||
|
}
|
||||||
|
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
|
||||||
|
struct args_struct {
|
||||||
|
size_t argc;
|
||||||
|
char** argv;
|
||||||
|
size_t elements_count = 0;
|
||||||
|
size_t index = 0;
|
||||||
|
|
||||||
|
std::vector<char> arg_chars = {};
|
||||||
|
std::vector<size_t> arg_idxs = {};
|
||||||
|
std::vector<char*> arg_ptrs = {};
|
||||||
|
|
||||||
|
args_struct() = default;
|
||||||
|
|
||||||
|
args_struct(char* file_name) {
|
||||||
|
createParams(file_name);
|
||||||
|
}
|
||||||
|
|
||||||
|
~args_struct() = default;
|
||||||
|
|
||||||
|
void reset() noexcept {
|
||||||
|
elements_count = 0;
|
||||||
|
index = 0;
|
||||||
|
|
||||||
|
arg_chars.clear();
|
||||||
|
arg_idxs.clear();
|
||||||
|
|
||||||
|
reset_args();
|
||||||
|
}
|
||||||
|
|
||||||
|
void reset_args() noexcept {
|
||||||
|
arg_ptrs.clear();
|
||||||
|
argc = 0;
|
||||||
|
argv = nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
void add(const std::string& data) {
|
||||||
|
// resetting previous args
|
||||||
|
reset_args();
|
||||||
|
|
||||||
|
arg_idxs.emplace_back(index);
|
||||||
|
for(const auto& character : data) {
|
||||||
|
arg_chars.emplace_back(character);
|
||||||
|
++index;
|
||||||
|
}
|
||||||
|
|
||||||
|
arg_chars.emplace_back('\0');
|
||||||
|
++index;
|
||||||
|
++elements_count;
|
||||||
|
}
|
||||||
|
|
||||||
|
void get_args() {
|
||||||
|
reset_args();
|
||||||
|
if(elements_count) {
|
||||||
|
arg_ptrs.reserve(elements_count);
|
||||||
|
|
||||||
|
for(const auto& index : arg_idxs) {
|
||||||
|
arg_ptrs.emplace_back(&arg_chars[index]);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
arg_ptrs.emplace_back(nullptr);
|
||||||
|
}
|
||||||
|
|
||||||
|
argc = elements_count;
|
||||||
|
argv = &arg_ptrs[0];
|
||||||
|
}
|
||||||
|
|
||||||
|
void createParams(char* file_name) {
|
||||||
|
reset(); // starting over
|
||||||
|
nlohmann::json file_config = get_json(file_name);
|
||||||
|
if (!file_config.empty()) { // ensures no unnecessary work
|
||||||
|
add(file_name);
|
||||||
|
for (auto& p : file_config.items()) {
|
||||||
|
// only use strings, numbers and booleans for switches
|
||||||
|
if (p.value().is_string() || p.value().is_number() || p.value().is_boolean()) {
|
||||||
|
add(p.key());
|
||||||
|
|
||||||
|
if (!p.value().is_boolean()) {
|
||||||
|
std::string param_value;
|
||||||
|
if (p.value().is_string()) {
|
||||||
|
param_value = p.value().get<std::string>();
|
||||||
|
} else if (p.value().is_number()) {
|
||||||
|
param_value = std::to_string(p.value().get<float>()); // works for int values too
|
||||||
|
}
|
||||||
|
add(param_value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
get_args();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
|
@ -1,6 +1,7 @@
|
||||||
#include "common.h"
|
#include "common.h"
|
||||||
#include "llama.h"
|
#include "llama.h"
|
||||||
#include "grammar-parser.h"
|
#include "grammar-parser.h"
|
||||||
|
#include "json-util.hpp"
|
||||||
|
|
||||||
#include "../llava/clip.h"
|
#include "../llava/clip.h"
|
||||||
|
|
||||||
|
@ -2319,7 +2320,14 @@ static void server_params_parse(int argc, char **argv, server_params &sparams,
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
if (!gpt_params_parse_json(argv[i], params)) { // attempt to read as a file
|
args_struct args_obj(argv[i]);
|
||||||
|
|
||||||
|
if (args_obj.argc > 0) {
|
||||||
|
int argc_json = args_obj.argc;
|
||||||
|
char** args_json = args_obj.argv;
|
||||||
|
|
||||||
|
return server_params_parse(argc_json, args_json, sparams, params, llama);
|
||||||
|
} else {
|
||||||
fprintf(stderr, "error: unknown argument: %s\n", arg.c_str());
|
fprintf(stderr, "error: unknown argument: %s\n", arg.c_str());
|
||||||
server_print_usage(argv[0], default_params, default_sparams);
|
server_print_usage(argv[0], default_params, default_sparams);
|
||||||
exit(1);
|
exit(1);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue