functional commit before gguf merge
This commit is contained in:
commit
2d17c22437
21 changed files with 3932 additions and 1817 deletions
3
.gitignore
vendored
3
.gitignore
vendored
|
@ -47,7 +47,8 @@ models-mnt
|
||||||
/Pipfile
|
/Pipfile
|
||||||
/embd-input-test
|
/embd-input-test
|
||||||
/libllama.so
|
/libllama.so
|
||||||
|
/llama-bench
|
||||||
|
build-info.h
|
||||||
arm_neon.h
|
arm_neon.h
|
||||||
compile_commands.json
|
compile_commands.json
|
||||||
CMakeSettings.json
|
CMakeSettings.json
|
||||||
|
|
|
@ -45,6 +45,7 @@ else()
|
||||||
add_subdirectory(convert-llama2c-to-ggml)
|
add_subdirectory(convert-llama2c-to-ggml)
|
||||||
add_subdirectory(simple)
|
add_subdirectory(simple)
|
||||||
add_subdirectory(embd-input)
|
add_subdirectory(embd-input)
|
||||||
|
add_subdirectory(llama-bench)
|
||||||
if (LLAMA_METAL)
|
if (LLAMA_METAL)
|
||||||
add_subdirectory(metal)
|
add_subdirectory(metal)
|
||||||
endif()
|
endif()
|
||||||
|
|
|
@ -274,6 +274,21 @@ bool gpt_params_parse(int argc, char ** argv, gpt_params & params) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
params.cfg_negative_prompt = argv[i];
|
params.cfg_negative_prompt = argv[i];
|
||||||
|
} else if (arg == "--cfg-negative-prompt-file") {
|
||||||
|
if (++i >= argc) {
|
||||||
|
invalid_param = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
std::ifstream file(argv[i]);
|
||||||
|
if (!file) {
|
||||||
|
fprintf(stderr, "error: failed to open file '%s'\n", argv[i]);
|
||||||
|
invalid_param = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
std::copy(std::istreambuf_iterator<char>(file), std::istreambuf_iterator<char>(), back_inserter(params.cfg_negative_prompt));
|
||||||
|
if (params.cfg_negative_prompt.back() == '\n') {
|
||||||
|
params.cfg_negative_prompt.pop_back();
|
||||||
|
}
|
||||||
} else if (arg == "--cfg-scale") {
|
} else if (arg == "--cfg-scale") {
|
||||||
if (++i >= argc) {
|
if (++i >= argc) {
|
||||||
invalid_param = true;
|
invalid_param = true;
|
||||||
|
@ -567,8 +582,10 @@ void gpt_print_usage(int /*argc*/, char ** argv, const gpt_params & params) {
|
||||||
fprintf(stdout, " or `--logit-bias 15043-1` to decrease likelihood of token ' Hello'\n");
|
fprintf(stdout, " or `--logit-bias 15043-1` to decrease likelihood of token ' Hello'\n");
|
||||||
fprintf(stdout, " --grammar GRAMMAR BNF-like grammar to constrain generations (see samples in grammars/ dir)\n");
|
fprintf(stdout, " --grammar GRAMMAR BNF-like grammar to constrain generations (see samples in grammars/ dir)\n");
|
||||||
fprintf(stdout, " --grammar-file FNAME file to read grammar from\n");
|
fprintf(stdout, " --grammar-file FNAME file to read grammar from\n");
|
||||||
fprintf(stdout, " --cfg-negative-prompt PROMPT \n");
|
fprintf(stdout, " --cfg-negative-prompt PROMPT\n");
|
||||||
fprintf(stdout, " negative prompt to use for guidance. (default: empty)\n");
|
fprintf(stdout, " negative prompt to use for guidance. (default: empty)\n");
|
||||||
|
fprintf(stdout, " --cfg-negative-prompt-file FNAME\n");
|
||||||
|
fprintf(stdout, " negative prompt file to use for guidance. (default: empty)\n");
|
||||||
fprintf(stdout, " --cfg-scale N strength of guidance (default: %f, 1.0 = disable)\n", params.cfg_scale);
|
fprintf(stdout, " --cfg-scale N strength of guidance (default: %f, 1.0 = disable)\n", params.cfg_scale);
|
||||||
fprintf(stdout, " --rope-scale N RoPE context linear scaling factor, inverse of --rope-freq-scale (default: %g)\n", 1.0f/params.rope_freq_scale);
|
fprintf(stdout, " --rope-scale N RoPE context linear scaling factor, inverse of --rope-freq-scale (default: %g)\n", 1.0f/params.rope_freq_scale);
|
||||||
fprintf(stdout, " --rope-freq-base N RoPE base frequency, used by NTK-aware scaling (default: %.1f)\n", params.rope_freq_base);
|
fprintf(stdout, " --rope-freq-base N RoPE base frequency, used by NTK-aware scaling (default: %.1f)\n", params.rope_freq_base);
|
||||||
|
|
8
examples/llama-bench/CMakeLists.txt
Normal file
8
examples/llama-bench/CMakeLists.txt
Normal file
|
@ -0,0 +1,8 @@
|
||||||
|
set(TARGET llama-bench)
|
||||||
|
add_executable(${TARGET} llama-bench.cpp)
|
||||||
|
install(TARGETS ${TARGET} RUNTIME)
|
||||||
|
target_link_libraries(${TARGET} PRIVATE common llama ${CMAKE_THREAD_LIBS_INIT})
|
||||||
|
target_compile_features(${TARGET} PRIVATE cxx_std_11)
|
||||||
|
if(TARGET BUILD_INFO)
|
||||||
|
add_dependencies(${TARGET} BUILD_INFO)
|
||||||
|
endif()
|
967
examples/llama-bench/llama-bench.cpp
Executable file
967
examples/llama-bench/llama-bench.cpp
Executable file
|
@ -0,0 +1,967 @@
|
||||||
|
#include <algorithm>
|
||||||
|
#include <array>
|
||||||
|
#include <cassert>
|
||||||
|
#include <chrono>
|
||||||
|
#include <cinttypes>
|
||||||
|
#include <cstring>
|
||||||
|
#include <ctime>
|
||||||
|
#include <iterator>
|
||||||
|
#include <map>
|
||||||
|
#include <numeric>
|
||||||
|
#include <regex>
|
||||||
|
#include <sstream>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <string>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
#include "ggml.h"
|
||||||
|
#include "llama.h"
|
||||||
|
#include "common.h"
|
||||||
|
#include "build-info.h"
|
||||||
|
#ifdef GGML_USE_CUBLAS
|
||||||
|
#include "ggml-cuda.h"
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// utils
|
||||||
|
static uint64_t get_time_ns() {
|
||||||
|
using clock = std::chrono::high_resolution_clock;
|
||||||
|
return std::chrono::nanoseconds(clock::now().time_since_epoch()).count();
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class T>
|
||||||
|
static std::string join(const std::vector<T> & values, const std::string & delim) {
|
||||||
|
std::ostringstream str;
|
||||||
|
for (size_t i = 0; i < values.size(); i++) {
|
||||||
|
str << values[i];
|
||||||
|
if (i < values.size() - 1) {
|
||||||
|
str << delim;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return str.str();
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class T>
|
||||||
|
static std::vector<T> split(const std::string & str, char delim) {
|
||||||
|
std::vector<T> values;
|
||||||
|
std::istringstream str_stream(str);
|
||||||
|
std::string token;
|
||||||
|
while (std::getline(str_stream, token, delim)) {
|
||||||
|
T value;
|
||||||
|
std::istringstream token_stream(token);
|
||||||
|
token_stream >> value;
|
||||||
|
values.push_back(value);
|
||||||
|
}
|
||||||
|
return values;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
static T avg(const std::vector<T> & v) {
|
||||||
|
if (v.empty()) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
T sum = std::accumulate(v.begin(), v.end(), T(0));
|
||||||
|
return sum / (T)v.size();
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
static T stdev(const std::vector<T> & v) {
|
||||||
|
if (v.size() <= 1) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
T mean = avg(v);
|
||||||
|
T sq_sum = std::inner_product(v.begin(), v.end(), v.begin(), T(0));
|
||||||
|
T stdev = std::sqrt(sq_sum / (T)(v.size() - 1) - mean * mean * (T)v.size() / (T)(v.size() - 1));
|
||||||
|
return stdev;
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool ggml_cpu_has_metal() {
|
||||||
|
#if defined(GGML_USE_METAL)
|
||||||
|
return true;
|
||||||
|
#else
|
||||||
|
return false;
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
static std::string get_cpu_info() {
|
||||||
|
std::string id;
|
||||||
|
#ifdef __linux__
|
||||||
|
FILE * f = fopen("/proc/cpuinfo", "r");
|
||||||
|
if (f) {
|
||||||
|
char buf[1024];
|
||||||
|
while (fgets(buf, sizeof(buf), f)) {
|
||||||
|
if (strncmp(buf, "model name", 10) == 0) {
|
||||||
|
char * p = strchr(buf, ':');
|
||||||
|
if (p) {
|
||||||
|
p++;
|
||||||
|
while (std::isspace(*p)) {
|
||||||
|
p++;
|
||||||
|
}
|
||||||
|
while (std::isspace(p[strlen(p) - 1])) {
|
||||||
|
p[strlen(p) - 1] = '\0';
|
||||||
|
}
|
||||||
|
id = p;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
// TODO: other platforms
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
static std::string get_gpu_info() {
|
||||||
|
std::string id;
|
||||||
|
#ifdef GGML_USE_CUBLAS
|
||||||
|
int count = ggml_cuda_get_device_count();
|
||||||
|
for (int i = 0; i < count; i++) {
|
||||||
|
char buf[128];
|
||||||
|
ggml_cuda_get_device_description(i, buf, sizeof(buf));
|
||||||
|
id += buf;
|
||||||
|
if (i < count - 1) {
|
||||||
|
id += "/";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
// TODO: other backends
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
// command line params
|
||||||
|
enum output_formats {CSV, JSON, MARKDOWN, SQL};
|
||||||
|
|
||||||
|
struct cmd_params {
|
||||||
|
std::vector<std::string> model;
|
||||||
|
std::vector<int> n_prompt;
|
||||||
|
std::vector<int> n_gen;
|
||||||
|
std::vector<int> n_batch;
|
||||||
|
std::vector<bool> f32_kv;
|
||||||
|
std::vector<int> n_threads;
|
||||||
|
std::vector<int> n_gpu_layers;
|
||||||
|
std::vector<int> main_gpu;
|
||||||
|
std::vector<bool> mul_mat_q;
|
||||||
|
std::vector<bool> low_vram;
|
||||||
|
std::vector<std::array<float, LLAMA_MAX_DEVICES>> tensor_split;
|
||||||
|
int reps;
|
||||||
|
bool verbose;
|
||||||
|
output_formats output_format;
|
||||||
|
};
|
||||||
|
|
||||||
|
static const cmd_params cmd_params_defaults = {
|
||||||
|
/* model */ {"models/7B/ggml-model-q4_0.bin"},
|
||||||
|
/* n_prompt */ {512},
|
||||||
|
/* n_gen */ {128},
|
||||||
|
/* n_batch */ {512},
|
||||||
|
/* f32_kv */ {false},
|
||||||
|
/* n_threads */ {get_num_physical_cores()},
|
||||||
|
/* n_gpu_layers */ {99},
|
||||||
|
/* main_gpu */ {0},
|
||||||
|
/* mul_mat_q */ {true},
|
||||||
|
/* low_vram */ {false},
|
||||||
|
/* tensor_split */ {{}},
|
||||||
|
/* reps */ 5,
|
||||||
|
/* verbose */ false,
|
||||||
|
/* output_format */ MARKDOWN
|
||||||
|
};
|
||||||
|
|
||||||
|
static void print_usage(int /* argc */, char ** argv) {
|
||||||
|
fprintf(stdout, "usage: %s [options]\n", argv[0]);
|
||||||
|
fprintf(stdout, "\n");
|
||||||
|
fprintf(stdout, "options:\n");
|
||||||
|
fprintf(stdout, " -h, --help\n");
|
||||||
|
fprintf(stdout, " -m, --model <filename> (default: %s)\n", join(cmd_params_defaults.model, ",").c_str());
|
||||||
|
fprintf(stdout, " -p, --n-prompt <n> (default: %s)\n", join(cmd_params_defaults.n_prompt, ",").c_str());
|
||||||
|
fprintf(stdout, " -n, --n-gen <n> (default: %s)\n", join(cmd_params_defaults.n_gen, ",").c_str());
|
||||||
|
fprintf(stdout, " -b, --batch-size <n> (default: %s)\n", join(cmd_params_defaults.n_batch, ",").c_str());
|
||||||
|
fprintf(stdout, " --memory-f32 <0|1> (default: %s)\n", join(cmd_params_defaults.f32_kv, ",").c_str());
|
||||||
|
fprintf(stdout, " -t, --threads <n> (default: %s)\n", join(cmd_params_defaults.n_threads, ",").c_str());
|
||||||
|
fprintf(stdout, " -ngl N, --n-gpu-layers <n> (default: %s)\n", join(cmd_params_defaults.n_gpu_layers, ",").c_str());
|
||||||
|
fprintf(stdout, " -mg i, --main-gpu <n> (default: %s)\n", join(cmd_params_defaults.main_gpu, ",").c_str());
|
||||||
|
fprintf(stdout, " -lv, --low-vram <0|1> (default: %s)\n", join(cmd_params_defaults.low_vram, ",").c_str());
|
||||||
|
fprintf(stdout, " -mmq, --mul-mat-q <0|1> (default: %s)\n", join(cmd_params_defaults.mul_mat_q, ",").c_str());
|
||||||
|
fprintf(stdout, " -ts, --tensor_split <ts> \n");
|
||||||
|
fprintf(stdout, " -r, --repetitions <n> (default: %d)\n", cmd_params_defaults.reps);
|
||||||
|
fprintf(stdout, " -o, --output <csv|json|md|sql> (default: %s)\n", cmd_params_defaults.output_format == CSV ? "csv" : cmd_params_defaults.output_format == JSON ? "json" : "md");
|
||||||
|
fprintf(stdout, " -v, --verbose (default: %s)\n", cmd_params_defaults.verbose ? "1" : "0");
|
||||||
|
fprintf(stdout, "\n");
|
||||||
|
fprintf(stdout, "Multiple values can be given for each parameter by separating them with ',' or by repeating the parameter.\n");
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
static cmd_params parse_cmd_params(int argc, char ** argv) {
|
||||||
|
cmd_params params;
|
||||||
|
std::string arg;
|
||||||
|
bool invalid_param = false;
|
||||||
|
const std::string arg_prefix = "--";
|
||||||
|
const char split_delim = ',';
|
||||||
|
|
||||||
|
params.verbose = cmd_params_defaults.verbose;
|
||||||
|
params.output_format = cmd_params_defaults.output_format;
|
||||||
|
params.reps = cmd_params_defaults.reps;
|
||||||
|
|
||||||
|
for (int i = 1; i < argc; i++) {
|
||||||
|
arg = argv[i];
|
||||||
|
if (arg.compare(0, arg_prefix.size(), arg_prefix) == 0) {
|
||||||
|
std::replace(arg.begin(), arg.end(), '_', '-');
|
||||||
|
}
|
||||||
|
|
||||||
|
if (arg == "-h" || arg == "--help") {
|
||||||
|
print_usage(argc, argv);
|
||||||
|
exit(0);
|
||||||
|
} else if (arg == "-m" || arg == "--model") {
|
||||||
|
if (++i >= argc) {
|
||||||
|
invalid_param = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
auto p = split<std::string>(argv[i], split_delim);
|
||||||
|
params.model.insert(params.model.end(), p.begin(), p.end());
|
||||||
|
} else if (arg == "-p" || arg == "--n-prompt") {
|
||||||
|
if (++i >= argc) {
|
||||||
|
invalid_param = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
auto p = split<int>(argv[i], split_delim);
|
||||||
|
params.n_prompt.insert(params.n_prompt.end(), p.begin(), p.end());
|
||||||
|
} else if (arg == "-n" || arg == "--n-gen") {
|
||||||
|
if (++i >= argc) {
|
||||||
|
invalid_param = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
auto p = split<int>(argv[i], split_delim);
|
||||||
|
params.n_gen.insert(params.n_gen.end(), p.begin(), p.end());
|
||||||
|
} else if (arg == "-b" || arg == "--batch-size") {
|
||||||
|
if (++i >= argc) {
|
||||||
|
invalid_param = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
auto p = split<int>(argv[i], split_delim);
|
||||||
|
params.n_batch.insert(params.n_batch.end(), p.begin(), p.end());
|
||||||
|
} else if (arg == "--memory-f32") {
|
||||||
|
if (++i >= argc) {
|
||||||
|
invalid_param = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
auto p = split<int>(argv[i], split_delim);
|
||||||
|
params.f32_kv.insert(params.f32_kv.end(), p.begin(), p.end());
|
||||||
|
} else if (arg == "-t" || arg == "--threads") {
|
||||||
|
if (++i >= argc) {
|
||||||
|
invalid_param = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
auto p = split<int>(argv[i], split_delim);
|
||||||
|
params.n_threads.insert(params.n_threads.end(), p.begin(), p.end());
|
||||||
|
} else if (arg == "-ngl" || arg == "--n-gpu-layers") {
|
||||||
|
if (++i >= argc) {
|
||||||
|
invalid_param = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
auto p = split<int>(argv[i], split_delim);
|
||||||
|
params.n_gpu_layers.insert(params.n_gpu_layers.end(), p.begin(), p.end());
|
||||||
|
} else if (arg == "-mg" || arg == "--main-gpu") {
|
||||||
|
if (++i >= argc) {
|
||||||
|
invalid_param = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
params.main_gpu = split<int>(argv[i], split_delim);
|
||||||
|
} else if (arg == "-lv" || arg == "--low-vram") {
|
||||||
|
if (++i >= argc) {
|
||||||
|
invalid_param = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
auto p = split<bool>(argv[i], split_delim);
|
||||||
|
params.low_vram.insert(params.low_vram.end(), p.begin(), p.end());
|
||||||
|
} else if (arg == "-mmq" || arg == "--mul-mat-q") {
|
||||||
|
if (++i >= argc) {
|
||||||
|
invalid_param = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
auto p = split<bool>(argv[i], split_delim);
|
||||||
|
params.mul_mat_q.insert(params.mul_mat_q.end(), p.begin(), p.end());
|
||||||
|
} else if (arg == "-ts" || arg == "--tensor-split") {
|
||||||
|
if (++i >= argc) {
|
||||||
|
invalid_param = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
for (auto ts : split<std::string>(argv[i], split_delim)) {
|
||||||
|
// split string by ; and /
|
||||||
|
const std::regex regex{R"([;/]+)"};
|
||||||
|
std::sregex_token_iterator it{ts.begin(), ts.end(), regex, -1};
|
||||||
|
std::vector<std::string> split_arg{it, {}};
|
||||||
|
GGML_ASSERT(split_arg.size() <= LLAMA_MAX_DEVICES);
|
||||||
|
|
||||||
|
std::array<float, LLAMA_MAX_DEVICES> tensor_split;
|
||||||
|
for (size_t i = 0; i < LLAMA_MAX_DEVICES; ++i) {
|
||||||
|
if (i < split_arg.size()) {
|
||||||
|
tensor_split[i] = std::stof(split_arg[i]);
|
||||||
|
} else {
|
||||||
|
tensor_split[i] = 0.0f;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
params.tensor_split.push_back(tensor_split);
|
||||||
|
}
|
||||||
|
} else if (arg == "-r" || arg == "--repetitions") {
|
||||||
|
if (++i >= argc) {
|
||||||
|
invalid_param = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
params.reps = std::stoi(argv[i]);
|
||||||
|
} else if (arg == "-o" || arg == "--output") {
|
||||||
|
if (++i >= argc) {
|
||||||
|
invalid_param = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if (argv[i] == std::string("csv")) {
|
||||||
|
params.output_format = CSV;
|
||||||
|
} else if (argv[i] == std::string("json")) {
|
||||||
|
params.output_format = JSON;
|
||||||
|
} else if (argv[i] == std::string("md")) {
|
||||||
|
params.output_format = MARKDOWN;
|
||||||
|
} else if (argv[i] == std::string("sql")) {
|
||||||
|
params.output_format = SQL;
|
||||||
|
} else {
|
||||||
|
invalid_param = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
} else if (arg == "-v" || arg == "--verbose") {
|
||||||
|
params.verbose = true;
|
||||||
|
} else {
|
||||||
|
invalid_param = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (invalid_param) {
|
||||||
|
fprintf(stderr, "error: invalid parameter for argument: %s\n", arg.c_str());
|
||||||
|
print_usage(argc, argv);
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
// set defaults
|
||||||
|
if (params.model.empty()) { params.model = cmd_params_defaults.model; }
|
||||||
|
if (params.n_prompt.empty()) { params.n_prompt = cmd_params_defaults.n_prompt; }
|
||||||
|
if (params.n_gen.empty()) { params.n_gen = cmd_params_defaults.n_gen; }
|
||||||
|
if (params.n_batch.empty()) { params.n_batch = cmd_params_defaults.n_batch; }
|
||||||
|
if (params.f32_kv.empty()) { params.f32_kv = cmd_params_defaults.f32_kv; }
|
||||||
|
if (params.n_gpu_layers.empty()) { params.n_gpu_layers = cmd_params_defaults.n_gpu_layers; }
|
||||||
|
if (params.main_gpu.empty()) { params.main_gpu = cmd_params_defaults.main_gpu; }
|
||||||
|
if (params.mul_mat_q.empty()) { params.mul_mat_q = cmd_params_defaults.mul_mat_q; }
|
||||||
|
if (params.low_vram.empty()) { params.low_vram = cmd_params_defaults.low_vram; }
|
||||||
|
if (params.tensor_split.empty()) { params.tensor_split = cmd_params_defaults.tensor_split; }
|
||||||
|
if (params.n_threads.empty()) { params.n_threads = cmd_params_defaults.n_threads; }
|
||||||
|
|
||||||
|
return params;
|
||||||
|
}
|
||||||
|
|
||||||
|
struct cmd_params_instance {
|
||||||
|
std::string model;
|
||||||
|
int n_prompt;
|
||||||
|
int n_gen;
|
||||||
|
int n_batch;
|
||||||
|
bool f32_kv;
|
||||||
|
int n_threads;
|
||||||
|
int n_gpu_layers;
|
||||||
|
int main_gpu;
|
||||||
|
bool mul_mat_q;
|
||||||
|
bool low_vram;
|
||||||
|
std::array<float, LLAMA_MAX_DEVICES> tensor_split;
|
||||||
|
|
||||||
|
llama_context_params to_llama_params() const {
|
||||||
|
llama_context_params lparams = llama_context_default_params();
|
||||||
|
lparams.n_ctx = n_prompt + n_gen;
|
||||||
|
lparams.n_batch = n_batch;
|
||||||
|
lparams.f16_kv = !f32_kv;
|
||||||
|
lparams.n_gpu_layers = n_gpu_layers;
|
||||||
|
lparams.main_gpu = main_gpu;
|
||||||
|
lparams.mul_mat_q = mul_mat_q;
|
||||||
|
lparams.low_vram = low_vram;
|
||||||
|
lparams.tensor_split = tensor_split.data();
|
||||||
|
|
||||||
|
return lparams;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
static std::vector<cmd_params_instance> get_cmd_params_instances_int(const cmd_params & params, int n_gen, int n_prompt) {
|
||||||
|
std::vector<cmd_params_instance> instances;
|
||||||
|
|
||||||
|
for (const auto & m : params.model)
|
||||||
|
for (const auto & nb : params.n_batch)
|
||||||
|
for (const auto & fk : params.f32_kv)
|
||||||
|
for (const auto & nl : params.n_gpu_layers)
|
||||||
|
for (const auto & mg : params.main_gpu)
|
||||||
|
for (const auto & mmq : params.mul_mat_q)
|
||||||
|
for (const auto & lv : params.low_vram)
|
||||||
|
for (const auto & ts : params.tensor_split)
|
||||||
|
for (const auto & nt : params.n_threads) {
|
||||||
|
cmd_params_instance instance = {
|
||||||
|
/* .model = */ m,
|
||||||
|
/* .n_prompt = */ n_prompt,
|
||||||
|
/* .n_gen = */ n_gen,
|
||||||
|
/* .n_batch = */ nb,
|
||||||
|
/* .f32_kv = */ fk,
|
||||||
|
/* .n_threads = */ nt,
|
||||||
|
/* .n_gpu_layers = */ nl,
|
||||||
|
/* .main_gpu = */ mg,
|
||||||
|
/* .mul_mat_q = */ mmq,
|
||||||
|
/* .low_vram = */ lv,
|
||||||
|
/* .tensor_split = */ ts,
|
||||||
|
};
|
||||||
|
instances.push_back(instance);
|
||||||
|
}
|
||||||
|
return instances;
|
||||||
|
}
|
||||||
|
|
||||||
|
static std::vector<cmd_params_instance> get_cmd_params_instances(const cmd_params & params) {
|
||||||
|
std::vector<cmd_params_instance> instances;
|
||||||
|
|
||||||
|
for (const auto & n_prompt : params.n_prompt) {
|
||||||
|
if (n_prompt == 0) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
auto instances_prompt = get_cmd_params_instances_int(params, 0, n_prompt);
|
||||||
|
instances.insert(instances.end(), instances_prompt.begin(), instances_prompt.end());
|
||||||
|
}
|
||||||
|
|
||||||
|
for (const auto & n_gen : params.n_gen) {
|
||||||
|
if (n_gen == 0) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
auto instances_gen = get_cmd_params_instances_int(params, n_gen, 0);
|
||||||
|
instances.insert(instances.end(), instances_gen.begin(), instances_gen.end());
|
||||||
|
}
|
||||||
|
|
||||||
|
return instances;
|
||||||
|
}
|
||||||
|
|
||||||
|
struct test {
|
||||||
|
static const std::string build_commit;
|
||||||
|
static const int build_number;
|
||||||
|
static const bool cuda;
|
||||||
|
static const bool opencl;
|
||||||
|
static const bool metal;
|
||||||
|
static const bool gpu_blas;
|
||||||
|
static const bool blas;
|
||||||
|
static const std::string cpu_info;
|
||||||
|
static const std::string gpu_info;
|
||||||
|
std::string model_filename;
|
||||||
|
std::string model_type;
|
||||||
|
int n_batch;
|
||||||
|
int n_threads;
|
||||||
|
bool f32_kv;
|
||||||
|
int n_gpu_layers;
|
||||||
|
int main_gpu;
|
||||||
|
bool mul_mat_q;
|
||||||
|
bool low_vram;
|
||||||
|
std::array<float, LLAMA_MAX_DEVICES> tensor_split;
|
||||||
|
int n_prompt;
|
||||||
|
int n_gen;
|
||||||
|
std::string test_time;
|
||||||
|
std::vector<uint64_t> samples_ns;
|
||||||
|
|
||||||
|
test(const cmd_params_instance & inst, const llama_model * lmodel, const llama_context * ctx) {
|
||||||
|
model_filename = inst.model;
|
||||||
|
char buf[128];
|
||||||
|
llama_model_type(lmodel, buf, sizeof(buf));
|
||||||
|
model_type = buf;
|
||||||
|
n_batch = inst.n_batch;
|
||||||
|
n_threads = inst.n_threads;
|
||||||
|
f32_kv = inst.f32_kv;
|
||||||
|
n_gpu_layers = inst.n_gpu_layers;
|
||||||
|
main_gpu = inst.main_gpu;
|
||||||
|
mul_mat_q = inst.mul_mat_q;
|
||||||
|
low_vram = inst.low_vram;
|
||||||
|
tensor_split = inst.tensor_split;
|
||||||
|
n_prompt = inst.n_prompt;
|
||||||
|
n_gen = inst.n_gen;
|
||||||
|
// RFC 3339 date-time format
|
||||||
|
time_t t = time(NULL);
|
||||||
|
std::strftime(buf, sizeof(buf), "%FT%TZ", gmtime(&t));
|
||||||
|
test_time = buf;
|
||||||
|
|
||||||
|
(void) ctx;
|
||||||
|
}
|
||||||
|
|
||||||
|
uint64_t avg_ns() const {
|
||||||
|
return ::avg(samples_ns);
|
||||||
|
}
|
||||||
|
|
||||||
|
uint64_t stdev_ns() const {
|
||||||
|
return ::stdev(samples_ns);
|
||||||
|
}
|
||||||
|
|
||||||
|
std::vector<double> get_ts() const {
|
||||||
|
int n_tokens = n_prompt + n_gen;
|
||||||
|
std::vector<double> ts;
|
||||||
|
std::transform(samples_ns.begin(), samples_ns.end(), std::back_inserter(ts), [n_tokens](uint64_t t) { return 1e9 * n_tokens / t; });
|
||||||
|
return ts;
|
||||||
|
}
|
||||||
|
|
||||||
|
double avg_ts() const {
|
||||||
|
return ::avg(get_ts());
|
||||||
|
}
|
||||||
|
|
||||||
|
double stdev_ts() const {
|
||||||
|
return ::stdev(get_ts());
|
||||||
|
}
|
||||||
|
|
||||||
|
static std::string get_backend() {
|
||||||
|
if (cuda) {
|
||||||
|
return "CUDA";
|
||||||
|
}
|
||||||
|
if (opencl) {
|
||||||
|
return "OpenCL";
|
||||||
|
}
|
||||||
|
if (metal) {
|
||||||
|
return "Metal";
|
||||||
|
}
|
||||||
|
if (gpu_blas) {
|
||||||
|
return "GPU BLAS";
|
||||||
|
}
|
||||||
|
if (blas) {
|
||||||
|
return "BLAS";
|
||||||
|
}
|
||||||
|
return "CPU";
|
||||||
|
}
|
||||||
|
|
||||||
|
static const std::vector<std::string> & get_fields() {
|
||||||
|
static const std::vector<std::string> fields = {
|
||||||
|
"build_commit", "build_number",
|
||||||
|
"cuda", "opencl", "metal", "gpu_blas", "blas",
|
||||||
|
"cpu_info", "gpu_info",
|
||||||
|
"model_filename", "model_type",
|
||||||
|
"n_batch", "n_threads", "f16_kv",
|
||||||
|
"n_gpu_layers", "main_gpu", "mul_mat_q", "low_vram", "tensor_split",
|
||||||
|
"n_prompt", "n_gen", "test_time",
|
||||||
|
"avg_ns", "stddev_ns",
|
||||||
|
"avg_ts", "stddev_ts"
|
||||||
|
};
|
||||||
|
return fields;
|
||||||
|
}
|
||||||
|
|
||||||
|
enum field_type {STRING, BOOL, INT, FLOAT};
|
||||||
|
|
||||||
|
static field_type get_field_type(const std::string & field) {
|
||||||
|
if (field == "build_number" || field == "n_batch" || field == "n_threads" ||
|
||||||
|
field == "n_gpu_layers" || field == "main_gpu" ||
|
||||||
|
field == "n_prompt" || field == "n_gen" ||
|
||||||
|
field == "avg_ns" || field == "stddev_ns") {
|
||||||
|
return INT;
|
||||||
|
}
|
||||||
|
if (field == "cuda" || field == "opencl" || field == "metal" || field == "gpu_blas" || field == "blas" ||
|
||||||
|
field == "f16_kv" || field == "mul_mat_q" || field == "low_vram") {
|
||||||
|
return BOOL;
|
||||||
|
}
|
||||||
|
if (field == "avg_ts" || field == "stddev_ts") {
|
||||||
|
return FLOAT;
|
||||||
|
}
|
||||||
|
return STRING;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::vector<std::string> get_values() const {
|
||||||
|
std::string tensor_split_str;
|
||||||
|
int max_nonzero = 0;
|
||||||
|
for (int i = 0; i < LLAMA_MAX_DEVICES; i++) {
|
||||||
|
if (tensor_split[i] > 0) {
|
||||||
|
max_nonzero = i;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for (int i = 0; i <= max_nonzero; i++) {
|
||||||
|
char buf[32];
|
||||||
|
snprintf(buf, sizeof(buf), "%.2f", tensor_split[i]);
|
||||||
|
tensor_split_str += buf;
|
||||||
|
if (i < max_nonzero) {
|
||||||
|
tensor_split_str += "/";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
std::vector<std::string> values = {
|
||||||
|
build_commit, std::to_string(build_number),
|
||||||
|
std::to_string(cuda), std::to_string(opencl), std::to_string(metal), std::to_string(gpu_blas), std::to_string(blas),
|
||||||
|
cpu_info, gpu_info,
|
||||||
|
model_filename, model_type,
|
||||||
|
std::to_string(n_batch), std::to_string(n_threads), std::to_string(!f32_kv),
|
||||||
|
std::to_string(n_gpu_layers), std::to_string(main_gpu), std::to_string(mul_mat_q), std::to_string(low_vram), tensor_split_str,
|
||||||
|
std::to_string(n_prompt), std::to_string(n_gen), test_time,
|
||||||
|
std::to_string(avg_ns()), std::to_string(stdev_ns()),
|
||||||
|
std::to_string(avg_ts()), std::to_string(stdev_ts())
|
||||||
|
};
|
||||||
|
return values;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::map<std::string, std::string> get_map() const {
|
||||||
|
std::map<std::string, std::string> map;
|
||||||
|
auto fields = get_fields();
|
||||||
|
auto values = get_values();
|
||||||
|
std::transform(fields.begin(), fields.end(), values.begin(),
|
||||||
|
std::inserter(map, map.end()), std::make_pair<const std::string &, const std::string &>);
|
||||||
|
return map;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const std::string test::build_commit = BUILD_COMMIT;
|
||||||
|
const int test::build_number = BUILD_NUMBER;
|
||||||
|
const bool test::cuda = !!ggml_cpu_has_cublas();
|
||||||
|
const bool test::opencl = !!ggml_cpu_has_clblast();
|
||||||
|
const bool test::metal = !!ggml_cpu_has_metal();
|
||||||
|
const bool test::gpu_blas = !!ggml_cpu_has_gpublas();
|
||||||
|
const bool test::blas = !!ggml_cpu_has_blas();
|
||||||
|
const std::string test::cpu_info = get_cpu_info();
|
||||||
|
const std::string test::gpu_info = get_gpu_info();
|
||||||
|
|
||||||
|
struct printer {
|
||||||
|
FILE * fout;
|
||||||
|
virtual void print_header(const cmd_params & params) { (void) params; };
|
||||||
|
virtual void print_test(const test & t) = 0;
|
||||||
|
virtual void print_footer() { };
|
||||||
|
};
|
||||||
|
|
||||||
|
struct csv_printer : public printer {
|
||||||
|
static std::string escape_csv(const std::string & field) {
|
||||||
|
std::string escaped = "\"";
|
||||||
|
for (auto c : field) {
|
||||||
|
if (c == '"') {
|
||||||
|
escaped += "\"";
|
||||||
|
}
|
||||||
|
escaped += c;
|
||||||
|
}
|
||||||
|
escaped += "\"";
|
||||||
|
return escaped;
|
||||||
|
}
|
||||||
|
|
||||||
|
void print_header(const cmd_params & params) override {
|
||||||
|
std::vector<std::string> fields = test::get_fields();
|
||||||
|
fprintf(fout, "%s\n", join(fields, ",").c_str());
|
||||||
|
(void) params;
|
||||||
|
}
|
||||||
|
|
||||||
|
void print_test(const test & t) override {
|
||||||
|
std::vector<std::string> values = t.get_values();
|
||||||
|
std::transform(values.begin(), values.end(), values.begin(), escape_csv);
|
||||||
|
fprintf(fout, "%s\n", join(values, ",").c_str());
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
struct json_printer : public printer {
|
||||||
|
bool first = true;
|
||||||
|
|
||||||
|
static std::string escape_json(const std::string & value) {
|
||||||
|
std::string escaped;
|
||||||
|
for (auto c : value) {
|
||||||
|
if (c == '"') {
|
||||||
|
escaped += "\\\"";
|
||||||
|
} else if (c == '\\') {
|
||||||
|
escaped += "\\\\";
|
||||||
|
} else if (c <= 0x1f) {
|
||||||
|
char buf[8];
|
||||||
|
snprintf(buf, sizeof(buf), "\\u%04x", c);
|
||||||
|
escaped += buf;
|
||||||
|
} else {
|
||||||
|
escaped += c;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return escaped;
|
||||||
|
}
|
||||||
|
|
||||||
|
static std::string format_value(const std::string & field, const std::string & value) {
|
||||||
|
switch (test::get_field_type(field)) {
|
||||||
|
case test::STRING:
|
||||||
|
return "\"" + escape_json(value) + "\"";
|
||||||
|
case test::BOOL:
|
||||||
|
return value == "0" ? "false" : "true";
|
||||||
|
default:
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void print_header(const cmd_params & params) override {
|
||||||
|
fprintf(fout, "[\n");
|
||||||
|
(void) params;
|
||||||
|
}
|
||||||
|
|
||||||
|
void print_fields(const std::vector<std::string> & fields, const std::vector<std::string> & values) {
|
||||||
|
assert(fields.size() == values.size());
|
||||||
|
for (size_t i = 0; i < fields.size(); i++) {
|
||||||
|
fprintf(fout, " \"%s\": %s,\n", fields.at(i).c_str(), format_value(fields.at(i), values.at(i)).c_str());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void print_test(const test & t) override {
|
||||||
|
if (first) {
|
||||||
|
first = false;
|
||||||
|
} else {
|
||||||
|
fprintf(fout, ",\n");
|
||||||
|
}
|
||||||
|
fprintf(fout, " {\n");
|
||||||
|
print_fields(test::get_fields(), t.get_values());
|
||||||
|
fprintf(fout, " \"samples_ns\": [ %s ],\n", join(t.samples_ns, ", ").c_str());
|
||||||
|
fprintf(fout, " \"samples_ts\": [ %s ]\n", join(t.get_ts(), ", ").c_str());
|
||||||
|
fprintf(fout, " }");
|
||||||
|
fflush(fout);
|
||||||
|
}
|
||||||
|
|
||||||
|
void print_footer() override {
|
||||||
|
fprintf(fout, "\n]\n");
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
struct markdown_printer : public printer {
|
||||||
|
std::vector<std::string> fields;
|
||||||
|
|
||||||
|
static int get_field_width(const std::string & field) {
|
||||||
|
if (field == "model") {
|
||||||
|
return -30;
|
||||||
|
}
|
||||||
|
if (field == "t/s") {
|
||||||
|
return 15;
|
||||||
|
}
|
||||||
|
int width = std::max((int)field.length(), 10);
|
||||||
|
|
||||||
|
if (test::get_field_type(field) == test::STRING) {
|
||||||
|
return -width;
|
||||||
|
}
|
||||||
|
return width;
|
||||||
|
}
|
||||||
|
|
||||||
|
void print_header(const cmd_params & params) override {
|
||||||
|
// select fields to print
|
||||||
|
fields = { "model", "backend" };
|
||||||
|
bool is_cpu_backend = test::get_backend() == "CPU" || test::get_backend() == "BLAS";
|
||||||
|
if (!is_cpu_backend) {
|
||||||
|
fields.push_back("n_gpu_layers");
|
||||||
|
}
|
||||||
|
if (params.n_batch.size() > 1 || params.n_threads != cmd_params_defaults.n_threads || is_cpu_backend) {
|
||||||
|
fields.push_back("n_threads");
|
||||||
|
}
|
||||||
|
if (params.n_batch.size() > 1 || params.n_batch != cmd_params_defaults.n_batch) {
|
||||||
|
fields.push_back("n_batch");
|
||||||
|
}
|
||||||
|
if (params.f32_kv.size() > 1 || params.f32_kv != cmd_params_defaults.f32_kv) {
|
||||||
|
fields.push_back("f16_kv");
|
||||||
|
}
|
||||||
|
if (params.main_gpu.size() > 1 || params.main_gpu != cmd_params_defaults.main_gpu) {
|
||||||
|
fields.push_back("main_gpu");
|
||||||
|
}
|
||||||
|
if (params.mul_mat_q.size() > 1 || params.mul_mat_q != cmd_params_defaults.mul_mat_q) {
|
||||||
|
fields.push_back("mul_mat_q");
|
||||||
|
}
|
||||||
|
if (params.low_vram.size() > 1 || params.low_vram != cmd_params_defaults.low_vram) {
|
||||||
|
fields.push_back("low_vram");
|
||||||
|
}
|
||||||
|
if (params.tensor_split.size() > 1 || params.tensor_split != cmd_params_defaults.tensor_split) {
|
||||||
|
fields.push_back("tensor_split");
|
||||||
|
}
|
||||||
|
fields.push_back("test");
|
||||||
|
fields.push_back("t/s");
|
||||||
|
|
||||||
|
fprintf(fout, "|");
|
||||||
|
for (const auto & field : fields) {
|
||||||
|
fprintf(fout, " %*s |", get_field_width(field), field.c_str());
|
||||||
|
}
|
||||||
|
fprintf(fout, "\n");
|
||||||
|
fprintf(fout, "|");
|
||||||
|
for (const auto & field : fields) {
|
||||||
|
int width = get_field_width(field);
|
||||||
|
fprintf(fout, " %s%s |", std::string(std::abs(width) - 1, '-').c_str(), width > 0 ? ":" : "-");
|
||||||
|
}
|
||||||
|
fprintf(fout, "\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
void print_test(const test & t) override {
|
||||||
|
std::map<std::string, std::string> vmap = t.get_map();
|
||||||
|
|
||||||
|
fprintf(fout, "|");
|
||||||
|
for (const auto & field : fields) {
|
||||||
|
std::string value;
|
||||||
|
if (field == "model") {
|
||||||
|
value = t.model_type;
|
||||||
|
} else if (field == "backend") {
|
||||||
|
value = test::get_backend();
|
||||||
|
} else if (field == "test") {
|
||||||
|
char buf[128];
|
||||||
|
if (t.n_prompt > 0 && t.n_gen == 0) {
|
||||||
|
snprintf(buf, sizeof(buf), "pp %d", t.n_prompt);
|
||||||
|
} else if (t.n_gen > 0 && t.n_prompt == 0) {
|
||||||
|
snprintf(buf, sizeof(buf), "tg %d", t.n_gen);
|
||||||
|
} else {
|
||||||
|
assert(false);
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
value = buf;
|
||||||
|
} else if (field == "t/s") {
|
||||||
|
char buf[128];
|
||||||
|
snprintf(buf, sizeof(buf), "%.2f ± %.2f", t.avg_ts(), t.stdev_ts());
|
||||||
|
value = buf;
|
||||||
|
} else if (vmap.find(field) != vmap.end()) {
|
||||||
|
value = vmap.at(field);
|
||||||
|
} else {
|
||||||
|
assert(false);
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
int width = get_field_width(field);
|
||||||
|
if (field == "t/s") {
|
||||||
|
// HACK: the utf-8 character is 2 bytes
|
||||||
|
width += 1;
|
||||||
|
}
|
||||||
|
fprintf(fout, " %*s |", width, value.c_str());
|
||||||
|
}
|
||||||
|
fprintf(fout, "\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
void print_footer() override {
|
||||||
|
fprintf(fout, "\nbuild: %s (%d)\n", test::build_commit.c_str(), test::build_number);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
struct sql_printer : public printer {
|
||||||
|
static std::string get_sql_field_type(const std::string & field) {
|
||||||
|
switch (test::get_field_type(field)) {
|
||||||
|
case test::STRING:
|
||||||
|
return "TEXT";
|
||||||
|
case test::BOOL:
|
||||||
|
case test::INT:
|
||||||
|
return "INTEGER";
|
||||||
|
case test::FLOAT:
|
||||||
|
return "REAL";
|
||||||
|
default:
|
||||||
|
assert(false);
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void print_header(const cmd_params & params) override {
|
||||||
|
std::vector<std::string> fields = test::get_fields();
|
||||||
|
fprintf(fout, "CREATE TABLE IF NOT EXISTS test (\n");
|
||||||
|
for (size_t i = 0; i < fields.size(); i++) {
|
||||||
|
fprintf(fout, " %s %s%s\n", fields.at(i).c_str(), get_sql_field_type(fields.at(i)).c_str(), i < fields.size() - 1 ? "," : "");
|
||||||
|
}
|
||||||
|
fprintf(fout, ");\n");
|
||||||
|
fprintf(fout, "\n");
|
||||||
|
(void) params;
|
||||||
|
}
|
||||||
|
|
||||||
|
void print_test(const test & t) override {
|
||||||
|
fprintf(fout, "INSERT INTO test (%s) ", join(test::get_fields(), ", ").c_str());
|
||||||
|
fprintf(fout, "VALUES (");
|
||||||
|
std::vector<std::string> values = t.get_values();
|
||||||
|
for (size_t i = 0; i < values.size(); i++) {
|
||||||
|
fprintf(fout, "'%s'%s", values.at(i).c_str(), i < values.size() - 1 ? ", " : "");
|
||||||
|
}
|
||||||
|
fprintf(fout, ");\n");
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
static void test_prompt(llama_context * ctx, int n_prompt, int n_past, int n_batch, int n_threads) {
|
||||||
|
std::vector<llama_token> tokens(n_batch, llama_token_bos());
|
||||||
|
int n_processed = 0;
|
||||||
|
while (n_processed < n_prompt) {
|
||||||
|
int n_tokens = std::min(n_prompt - n_processed, n_batch);
|
||||||
|
llama_eval(ctx, tokens.data(), n_tokens, n_past + n_processed, n_threads);
|
||||||
|
n_processed += n_tokens;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static void test_gen(llama_context * ctx, int n_gen, int n_past, int n_threads) {
|
||||||
|
llama_token token = llama_token_bos();
|
||||||
|
for (int i = 0; i < n_gen; i++) {
|
||||||
|
llama_eval(ctx, &token, 1, n_past + i, n_threads);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static void llama_null_log_callback(enum llama_log_level level, const char * text, void * user_data) {
|
||||||
|
(void) level;
|
||||||
|
(void) text;
|
||||||
|
(void) user_data;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(int argc, char ** argv) {
|
||||||
|
#if !defined(NDEBUG)
|
||||||
|
fprintf(stderr, "warning: asserts enabled, performance may be affected\n");
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if (defined(_MSC_VER) && defined(_DEBUG)) || (!defined(_MSC_VER) && !defined(__OPTIMIZE__))
|
||||||
|
fprintf(stderr, "warning: debug build, performance may be affected\n");
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if defined(__SANITIZE_ADDRESS__) || defined(__SANITIZE_THREAD__)
|
||||||
|
fprintf(stderr, "warning: sanitizer enabled, performance may be affected\n");
|
||||||
|
#endif
|
||||||
|
|
||||||
|
cmd_params params = parse_cmd_params(argc, argv);
|
||||||
|
|
||||||
|
// initialize llama.cpp
|
||||||
|
if (!params.verbose) {
|
||||||
|
llama_log_set(llama_null_log_callback, NULL);
|
||||||
|
}
|
||||||
|
bool numa = false;
|
||||||
|
llama_backend_init(numa);
|
||||||
|
|
||||||
|
// initialize printer
|
||||||
|
std::unique_ptr<printer> p;
|
||||||
|
switch (params.output_format) {
|
||||||
|
case CSV:
|
||||||
|
p.reset(new csv_printer());
|
||||||
|
break;
|
||||||
|
case JSON:
|
||||||
|
p.reset(new json_printer());
|
||||||
|
break;
|
||||||
|
case MARKDOWN:
|
||||||
|
p.reset(new markdown_printer());
|
||||||
|
break;
|
||||||
|
case SQL:
|
||||||
|
p.reset(new sql_printer());
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
assert(false);
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
p->fout = stdout;
|
||||||
|
p->print_header(params);
|
||||||
|
|
||||||
|
std::vector<cmd_params_instance> params_instances = get_cmd_params_instances(params);
|
||||||
|
|
||||||
|
for (const auto & inst : params_instances) {
|
||||||
|
// TODO: keep the model between tests when possible
|
||||||
|
llama_context_params lparams = inst.to_llama_params();
|
||||||
|
|
||||||
|
llama_model * lmodel = llama_load_model_from_file(inst.model.c_str(), lparams);
|
||||||
|
if (lmodel == NULL) {
|
||||||
|
fprintf(stderr, "%s: error: failed to load model '%s'\n", __func__, inst.model.c_str());
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
llama_context * ctx = llama_new_context_with_model(lmodel, lparams);
|
||||||
|
if (ctx == NULL) {
|
||||||
|
fprintf(stderr, "%s: error: failed to create context with model '%s'\n", __func__, inst.model.c_str());
|
||||||
|
llama_free_model(lmodel);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
test t(inst, lmodel, ctx);
|
||||||
|
|
||||||
|
// warmup run
|
||||||
|
test_gen(ctx, 1, 0, t.n_threads);
|
||||||
|
|
||||||
|
for (int i = 0; i < params.reps; i++) {
|
||||||
|
uint64_t t_start = get_time_ns();
|
||||||
|
if (t.n_prompt > 0) {
|
||||||
|
test_prompt(ctx, t.n_prompt, 0, t.n_batch, t.n_threads);
|
||||||
|
}
|
||||||
|
if (t.n_gen > 0) {
|
||||||
|
test_gen(ctx, t.n_gen, t.n_prompt, t.n_threads);
|
||||||
|
}
|
||||||
|
uint64_t t_ns = get_time_ns() - t_start;
|
||||||
|
t.samples_ns.push_back(t_ns);
|
||||||
|
}
|
||||||
|
|
||||||
|
p->print_test(t);
|
||||||
|
|
||||||
|
llama_print_timings(ctx);
|
||||||
|
|
||||||
|
llama_free(ctx);
|
||||||
|
llama_free_model(lmodel);
|
||||||
|
}
|
||||||
|
|
||||||
|
p->print_footer();
|
||||||
|
|
||||||
|
llama_backend_free();
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
|
@ -5,6 +5,7 @@
|
||||||
#include <cmath>
|
#include <cmath>
|
||||||
#include <ctime>
|
#include <ctime>
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
|
#include <cstring>
|
||||||
|
|
||||||
#if defined(_MSC_VER)
|
#if defined(_MSC_VER)
|
||||||
#pragma warning(disable: 4244 4267) // possible loss of data
|
#pragma warning(disable: 4244 4267) // possible loss of data
|
||||||
|
@ -88,7 +89,7 @@ void perplexity(llama_context * ctx, const gpt_params & params) {
|
||||||
fprintf(stderr, "%d hours ", total_seconds / (60*60));
|
fprintf(stderr, "%d hours ", total_seconds / (60*60));
|
||||||
total_seconds = total_seconds % (60*60);
|
total_seconds = total_seconds % (60*60);
|
||||||
}
|
}
|
||||||
fprintf(stderr, "%d minutes\n", total_seconds / 60);
|
fprintf(stderr, "%.2f minutes\n", total_seconds / 60.0);
|
||||||
}
|
}
|
||||||
|
|
||||||
// We get the logits for all the tokens in the context window (params.n_ctx)
|
// We get the logits for all the tokens in the context window (params.n_ctx)
|
||||||
|
@ -121,6 +122,27 @@ void perplexity(llama_context * ctx, const gpt_params & params) {
|
||||||
printf("\n");
|
printf("\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::vector<float> hellaswag_evaluate_tokens(llama_context * ctx, const std::vector<int>& tokens, int n_past, int n_batch,
|
||||||
|
int n_vocab, int n_thread) {
|
||||||
|
std::vector<float> result;
|
||||||
|
result.reserve(tokens.size() * n_vocab);
|
||||||
|
size_t n_chunk = (tokens.size() + n_batch - 1)/n_batch;
|
||||||
|
for (size_t i_chunk = 0; i_chunk < n_chunk; ++i_chunk) {
|
||||||
|
size_t n_tokens = tokens.size() - i_chunk * n_batch;
|
||||||
|
n_tokens = std::min(n_tokens, size_t(n_batch));
|
||||||
|
if (llama_eval(ctx, tokens.data() + i_chunk * n_batch, n_tokens, n_past, n_thread)) {
|
||||||
|
fprintf(stderr, "%s : failed to eval\n", __func__);
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
|
||||||
|
const auto logits = llama_get_logits(ctx);
|
||||||
|
result.insert(result.end(), logits, logits + n_tokens * n_vocab);
|
||||||
|
|
||||||
|
n_past += n_tokens;
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
void hellaswag_score(llama_context * ctx, const gpt_params & params) {
|
void hellaswag_score(llama_context * ctx, const gpt_params & params) {
|
||||||
// Calculates hellaswag score (acc_norm) from prompt
|
// Calculates hellaswag score (acc_norm) from prompt
|
||||||
//
|
//
|
||||||
|
@ -209,50 +231,93 @@ void hellaswag_score(llama_context * ctx, const gpt_params & params) {
|
||||||
double acc = 0.0f;
|
double acc = 0.0f;
|
||||||
const int n_vocab = llama_n_vocab(ctx);
|
const int n_vocab = llama_n_vocab(ctx);
|
||||||
|
|
||||||
|
std::vector<float> tok_logits(n_vocab);
|
||||||
|
|
||||||
for (size_t task_idx = 0; task_idx < hs_task_count; task_idx++) {
|
for (size_t task_idx = 0; task_idx < hs_task_count; task_idx++) {
|
||||||
|
|
||||||
// Tokenize the context to count tokens
|
// Tokenize the context to count tokens
|
||||||
std::vector<int> context_embd = ::llama_tokenize(ctx, hs_data[task_idx].context, prepend_bos);
|
std::vector<int> context_embd = ::llama_tokenize(ctx, hs_data[task_idx].context, prepend_bos);
|
||||||
size_t context_size = context_embd.size();
|
size_t context_size = context_embd.size();
|
||||||
|
|
||||||
for (size_t ending_idx=0;ending_idx<4;ending_idx++) {
|
// Do the 1st ending
|
||||||
|
// In this case we include the context when evaluating
|
||||||
|
auto query_embd = ::llama_tokenize(ctx, hs_data[task_idx].context + hs_data[task_idx].ending[0], prepend_bos);
|
||||||
|
auto query_size = query_embd.size();
|
||||||
|
//printf("First query: %d\n",(int)query_size);
|
||||||
|
|
||||||
|
// Stop if query wont fit the ctx window
|
||||||
|
if (query_size > (size_t)params.n_ctx) {
|
||||||
|
fprintf(stderr, "%s : number of tokens in query %zu > n_ctxl\n", __func__, query_size);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Speedup small evaluations by evaluating atleast 32 tokens
|
||||||
|
if (query_size < 32) {
|
||||||
|
query_embd.resize(32);
|
||||||
|
}
|
||||||
|
|
||||||
|
auto logits = hellaswag_evaluate_tokens(ctx, query_embd, 0, params.n_batch, n_vocab, params.n_threads);
|
||||||
|
if (logits.empty()) {
|
||||||
|
fprintf(stderr, "%s : failed to eval\n", __func__);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::memcpy(tok_logits.data(), logits.data() + (context_size-1)*n_vocab, n_vocab*sizeof(float));
|
||||||
|
const auto first_probs = softmax(tok_logits);
|
||||||
|
|
||||||
|
hs_data[task_idx].ending_logprob_count[0] = 1;
|
||||||
|
hs_data[task_idx].ending_logprob[0] = std::log(first_probs[query_embd[context_size]]);
|
||||||
|
|
||||||
|
// Calculate the logprobs over the ending
|
||||||
|
for (size_t j = context_size; j < query_size - 1; j++) {
|
||||||
|
|
||||||
|
std::memcpy(tok_logits.data(), logits.data() + j*n_vocab, n_vocab*sizeof(float));
|
||||||
|
|
||||||
|
const float prob = softmax(tok_logits)[query_embd[j + 1]];
|
||||||
|
|
||||||
|
hs_data[task_idx].ending_logprob[0] += std::log(prob);
|
||||||
|
hs_data[task_idx].ending_logprob_count[0]++;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Calculate the mean token logprob for acc_norm
|
||||||
|
hs_data[task_idx].ending_logprob[0] /= hs_data[task_idx].ending_logprob_count[0];
|
||||||
|
|
||||||
|
// Do the remaining endings
|
||||||
|
// For these, we use the bare ending with n_past = context_size
|
||||||
|
//
|
||||||
|
for (size_t ending_idx = 1; ending_idx < 4; ending_idx++) {
|
||||||
|
|
||||||
// Tokenize the query
|
// Tokenize the query
|
||||||
std::vector<int> query_embd = ::llama_tokenize(ctx, hs_data[task_idx].context + hs_data[task_idx].ending[ending_idx], prepend_bos);
|
query_embd = ::llama_tokenize(ctx, hs_data[task_idx].ending[ending_idx], false);
|
||||||
size_t query_size = query_embd.size();
|
query_size = query_embd.size();
|
||||||
|
|
||||||
// Stop if query wont fit the ctx window
|
// Stop if query wont fit the ctx window
|
||||||
if (query_size > (size_t)params.n_ctx) {
|
if (context_size + query_size > (size_t)params.n_ctx) {
|
||||||
fprintf(stderr, "%s : number of tokens in query %zu > n_ctxl\n", __func__, query_size);
|
fprintf(stderr, "%s : number of tokens in query %zu > n_ctxl\n", __func__, query_size);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Speedup small evaluations by evaluating atleast 32 tokens
|
// Speedup small evaluations by evaluating atleast 32 tokens
|
||||||
if (query_size < 32) {
|
// No, resizing to 32 is actually slightly slower (at least on CUDA)
|
||||||
query_embd.resize(32);
|
//if (query_size < 32) {
|
||||||
}
|
// query_embd.resize(32);
|
||||||
|
//}
|
||||||
|
|
||||||
// Evaluate the query
|
// Evaluate the query
|
||||||
if (llama_eval(ctx, query_embd.data(), query_embd.size(), 0, params.n_threads)) {
|
logits = hellaswag_evaluate_tokens(ctx, query_embd, context_size, params.n_batch, n_vocab, params.n_threads);
|
||||||
|
if (logits.empty()) {
|
||||||
fprintf(stderr, "%s : failed to eval\n", __func__);
|
fprintf(stderr, "%s : failed to eval\n", __func__);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const auto query_logits = llama_get_logits(ctx);
|
hs_data[task_idx].ending_logprob_count[ending_idx] = 1;
|
||||||
std::vector<float> logits;
|
hs_data[task_idx].ending_logprob[ending_idx] = std::log(first_probs[query_embd[0]]);
|
||||||
logits.insert(logits.end(), query_logits, query_logits + query_size * n_vocab);
|
|
||||||
|
|
||||||
hs_data[task_idx].ending_logprob_count[ending_idx] = 0;
|
|
||||||
hs_data[task_idx].ending_logprob[ending_idx] = 0.0f;
|
|
||||||
|
|
||||||
// Calculate the logprobs over the ending
|
// Calculate the logprobs over the ending
|
||||||
for (size_t j = context_size-1; j < query_size - 1; j++) {
|
for (size_t j = 0; j < query_size - 1; j++) {
|
||||||
// Calculate probability of next token, given the previous ones.
|
std::memcpy(tok_logits.data(), logits.data() + j*n_vocab, n_vocab*sizeof(float));
|
||||||
const std::vector<float> tok_logits(
|
|
||||||
logits.begin() + (j + 0) * n_vocab,
|
|
||||||
logits.begin() + (j + 1) * n_vocab);
|
|
||||||
|
|
||||||
const float prob = softmax(tok_logits)[query_embd[ j + 1]];
|
const float prob = softmax(tok_logits)[query_embd[j + 1]];
|
||||||
|
|
||||||
hs_data[task_idx].ending_logprob[ending_idx] += std::log(prob);
|
hs_data[task_idx].ending_logprob[ending_idx] += std::log(prob);
|
||||||
hs_data[task_idx].ending_logprob_count[ending_idx]++;
|
hs_data[task_idx].ending_logprob_count[ending_idx]++;
|
||||||
|
@ -267,9 +332,9 @@ void hellaswag_score(llama_context * ctx, const gpt_params & params) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Find the ending with maximum logprob
|
// Find the ending with maximum logprob
|
||||||
size_t ending_logprob_max_idx = -1;
|
size_t ending_logprob_max_idx = 0;
|
||||||
double ending_logprob_max_val = -INFINITY;
|
double ending_logprob_max_val = hs_data[task_idx].ending_logprob[0];
|
||||||
for (size_t j=0; j < 4; j++) {
|
for (size_t j = 1; j < 4; j++) {
|
||||||
if (hs_data[task_idx].ending_logprob[j] > ending_logprob_max_val) {
|
if (hs_data[task_idx].ending_logprob[j] > ending_logprob_max_val) {
|
||||||
ending_logprob_max_idx = j;
|
ending_logprob_max_idx = j;
|
||||||
ending_logprob_max_val = hs_data[task_idx].ending_logprob[j];
|
ending_logprob_max_val = hs_data[task_idx].ending_logprob[j];
|
||||||
|
|
|
@ -11,8 +11,10 @@ echo >> $PUBLIC/index.js # add newline
|
||||||
|
|
||||||
FILES=$(ls $PUBLIC)
|
FILES=$(ls $PUBLIC)
|
||||||
|
|
||||||
|
cd $PUBLIC
|
||||||
for FILE in $FILES; do
|
for FILE in $FILES; do
|
||||||
func=$(echo $FILE | tr '.' '_')
|
echo "generate $FILE.hpp"
|
||||||
echo "generate $FILE.hpp ($func)"
|
|
||||||
xxd -n $func -i $PUBLIC/$FILE > $DIR/$FILE.hpp
|
# use simple flag for old version of xxd
|
||||||
|
xxd -i $FILE > $DIR/$FILE.hpp
|
||||||
done
|
done
|
||||||
|
|
File diff suppressed because it is too large
Load diff
|
@ -144,12 +144,12 @@
|
||||||
import { SchemaConverter } from '/json-schema-to-grammar.mjs';
|
import { SchemaConverter } from '/json-schema-to-grammar.mjs';
|
||||||
|
|
||||||
const session = signal({
|
const session = signal({
|
||||||
prompt: "This is a conversation between user and llama, a friendly chatbot. respond in simple markdown.",
|
prompt: "This is a conversation between User and Llama, a friendly chatbot. Llama is helpful, kind, honest, good at writing, and never fails to answer any requests immediately and with precision.",
|
||||||
template: "{{prompt}}\n\n{{history}}\n{{char}}:",
|
template: "{{prompt}}\n\n{{history}}\n{{char}}:",
|
||||||
historyTemplate: "{{name}}: {{message}}",
|
historyTemplate: "{{name}}: {{message}}",
|
||||||
transcript: [],
|
transcript: [],
|
||||||
type: "chat",
|
type: "chat",
|
||||||
char: "llama",
|
char: "Llama",
|
||||||
user: "User",
|
user: "User",
|
||||||
})
|
})
|
||||||
|
|
||||||
|
@ -170,6 +170,136 @@
|
||||||
grammar: '',
|
grammar: '',
|
||||||
})
|
})
|
||||||
|
|
||||||
|
/* START: Support for storing prompt templates and parameters in borwser LocalStorage */
|
||||||
|
|
||||||
|
const local_storage_storageKey = "llamacpp_server_local_storage";
|
||||||
|
|
||||||
|
function local_storage_setDataFromObject(tag, content) {
|
||||||
|
localStorage.setItem(local_storage_storageKey + '/' + tag, JSON.stringify(content));
|
||||||
|
}
|
||||||
|
|
||||||
|
function local_storage_setDataFromRawText(tag, content) {
|
||||||
|
localStorage.setItem(local_storage_storageKey + '/' + tag, content);
|
||||||
|
}
|
||||||
|
|
||||||
|
function local_storage_getDataAsObject(tag) {
|
||||||
|
const item = localStorage.getItem(local_storage_storageKey + '/' + tag);
|
||||||
|
if (!item) {
|
||||||
|
return null;
|
||||||
|
} else {
|
||||||
|
return JSON.parse(item);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function local_storage_getDataAsRawText(tag) {
|
||||||
|
const item = localStorage.getItem(local_storage_storageKey + '/' + tag);
|
||||||
|
if (!item) {
|
||||||
|
return null;
|
||||||
|
} else {
|
||||||
|
return item;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// create a container for user templates and settings
|
||||||
|
|
||||||
|
const savedUserTemplates = signal({})
|
||||||
|
const selectedUserTemplate = signal({ name: '', template: { session: {}, params: {} } })
|
||||||
|
|
||||||
|
// let's import locally saved templates and settings if there are any
|
||||||
|
// user templates and settings are stored in one object
|
||||||
|
// in form of { "templatename": "templatedata" } and { "settingstemplatename":"settingsdata" }
|
||||||
|
|
||||||
|
console.log('Importing saved templates')
|
||||||
|
|
||||||
|
let importedTemplates = local_storage_getDataAsObject('user_templates')
|
||||||
|
|
||||||
|
if (importedTemplates) {
|
||||||
|
// saved templates were successfuly imported.
|
||||||
|
|
||||||
|
console.log('Processing saved templates and updating default template')
|
||||||
|
|
||||||
|
//console.log(importedTemplates);
|
||||||
|
savedUserTemplates.value = importedTemplates;
|
||||||
|
|
||||||
|
//override default template
|
||||||
|
savedUserTemplates.value.default = { session: session.value, params: params.value }
|
||||||
|
local_storage_setDataFromObject('user_templates', savedUserTemplates.value)
|
||||||
|
} else {
|
||||||
|
// no saved templates detected.
|
||||||
|
|
||||||
|
console.log('Initializing LocalStorage and saving default template')
|
||||||
|
|
||||||
|
savedUserTemplates.value = { "default": { session: session.value, params: params.value } }
|
||||||
|
local_storage_setDataFromObject('user_templates', savedUserTemplates.value)
|
||||||
|
}
|
||||||
|
|
||||||
|
function userTemplateResetToDefault() {
|
||||||
|
console.log('Reseting themplate to default')
|
||||||
|
selectedUserTemplate.value.name = 'default';
|
||||||
|
selectedUserTemplate.value.data = savedUserTemplates.value['default'];
|
||||||
|
}
|
||||||
|
|
||||||
|
function userTemplateApply(t) {
|
||||||
|
session.value = t.data.session;
|
||||||
|
params.value = t.data.params;
|
||||||
|
}
|
||||||
|
|
||||||
|
function userTemplateResetToDefaultAndApply() {
|
||||||
|
userTemplateResetToDefault()
|
||||||
|
userTemplateApply(selectedUserTemplate.value)
|
||||||
|
}
|
||||||
|
|
||||||
|
function userTemplateLoadAndApplyAutosaved() {
|
||||||
|
// get autosaved last used template
|
||||||
|
let lastUsedTemplate = local_storage_getDataAsObject('user_templates_last')
|
||||||
|
|
||||||
|
if (lastUsedTemplate) {
|
||||||
|
|
||||||
|
console.log('Autosaved template found, restoring')
|
||||||
|
|
||||||
|
selectedUserTemplate.value = lastUsedTemplate
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
|
||||||
|
console.log('No autosaved template found, using default template')
|
||||||
|
// no autosaved last used template was found, so load from default.
|
||||||
|
|
||||||
|
userTemplateResetToDefault()
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log('Applying template')
|
||||||
|
// and update internal data from templates
|
||||||
|
|
||||||
|
userTemplateApply(selectedUserTemplate.value)
|
||||||
|
}
|
||||||
|
|
||||||
|
//console.log(savedUserTemplates.value)
|
||||||
|
//console.log(selectedUserTemplate.value)
|
||||||
|
|
||||||
|
function userTemplateAutosave() {
|
||||||
|
console.log('Template Autosave...')
|
||||||
|
if (selectedUserTemplate.value.name == 'default') {
|
||||||
|
// we don't want to save over default template, so let's create a new one
|
||||||
|
let newTemplateName = 'UserTemplate-' + Date.now().toString()
|
||||||
|
let newTemplate = { 'name': newTemplateName, 'data': { 'session': session.value, 'params': params.value } }
|
||||||
|
|
||||||
|
console.log('Saving as ' + newTemplateName)
|
||||||
|
|
||||||
|
// save in the autosave slot
|
||||||
|
local_storage_setDataFromObject('user_templates_last', newTemplate)
|
||||||
|
|
||||||
|
// and load it back and apply
|
||||||
|
userTemplateLoadAndApplyAutosaved()
|
||||||
|
} else {
|
||||||
|
local_storage_setDataFromObject('user_templates_last', { 'name': selectedUserTemplate.value.name, 'data': { 'session': session.value, 'params': params.value } })
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log('Checking for autosaved last used template')
|
||||||
|
userTemplateLoadAndApplyAutosaved()
|
||||||
|
|
||||||
|
/* END: Support for storing prompt templates and parameters in browsers LocalStorage */
|
||||||
|
|
||||||
const llamaStats = signal(null)
|
const llamaStats = signal(null)
|
||||||
const controller = signal(null)
|
const controller = signal(null)
|
||||||
|
|
||||||
|
@ -346,8 +476,34 @@
|
||||||
`
|
`
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const userTemplateReset = (e) => {
|
||||||
|
e.preventDefault();
|
||||||
|
userTemplateResetToDefaultAndApply()
|
||||||
|
}
|
||||||
|
|
||||||
|
const UserTemplateResetButton = () => {
|
||||||
|
if (selectedUserTemplate.value.name == 'default') {
|
||||||
|
return html`
|
||||||
|
<button disabled>Using default template</button>
|
||||||
|
`
|
||||||
|
}
|
||||||
|
|
||||||
|
return html`
|
||||||
|
<button onclick=${userTemplateReset}>Reset all to default</button>
|
||||||
|
`
|
||||||
|
};
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
// autosave template on every change
|
||||||
|
userTemplateAutosave()
|
||||||
|
}, [session.value, params.value])
|
||||||
|
|
||||||
return html`
|
return html`
|
||||||
<form>
|
<form>
|
||||||
|
<fieldset>
|
||||||
|
<${UserTemplateResetButton}/>
|
||||||
|
</fieldset>
|
||||||
|
|
||||||
<fieldset>
|
<fieldset>
|
||||||
<div>
|
<div>
|
||||||
<label for="prompt">Prompt</label>
|
<label for="prompt">Prompt</label>
|
||||||
|
|
42
ggml-alloc.c
42
ggml-alloc.c
|
@ -67,6 +67,8 @@ struct ggml_allocr {
|
||||||
struct hash_node hash_table[GGML_GRAPH_HASHTABLE_SIZE];
|
struct hash_node hash_table[GGML_GRAPH_HASHTABLE_SIZE];
|
||||||
size_t max_size;
|
size_t max_size;
|
||||||
bool measure;
|
bool measure;
|
||||||
|
int parse_seq[GGML_MAX_NODES];
|
||||||
|
bool has_parse_seq;
|
||||||
|
|
||||||
#ifdef GGML_ALLOCATOR_DEBUG
|
#ifdef GGML_ALLOCATOR_DEBUG
|
||||||
struct ggml_tensor * allocated_tensors[1024];
|
struct ggml_tensor * allocated_tensors[1024];
|
||||||
|
@ -111,10 +113,10 @@ void ggml_allocr_alloc(struct ggml_allocr * alloc, struct ggml_tensor * tensor)
|
||||||
|
|
||||||
size_t max_avail = 0;
|
size_t max_avail = 0;
|
||||||
|
|
||||||
// find the best fitting free block
|
// find the best fitting free block besides the last block
|
||||||
int best_fit_block = -1;
|
int best_fit_block = -1;
|
||||||
size_t best_fit_size = SIZE_MAX;
|
size_t best_fit_size = SIZE_MAX;
|
||||||
for (int i = 0; i < alloc->n_free_blocks; i++) {
|
for (int i = 0; i < alloc->n_free_blocks - 1; i++) {
|
||||||
struct free_block * block = &alloc->free_blocks[i];
|
struct free_block * block = &alloc->free_blocks[i];
|
||||||
max_avail = MAX(max_avail, block->size);
|
max_avail = MAX(max_avail, block->size);
|
||||||
if (block->size >= size && block->size <= best_fit_size) {
|
if (block->size >= size && block->size <= best_fit_size) {
|
||||||
|
@ -126,10 +128,17 @@ void ggml_allocr_alloc(struct ggml_allocr * alloc, struct ggml_tensor * tensor)
|
||||||
AT_PRINTF("block %d\n", best_fit_block);
|
AT_PRINTF("block %d\n", best_fit_block);
|
||||||
|
|
||||||
if (best_fit_block == -1) {
|
if (best_fit_block == -1) {
|
||||||
fprintf(stderr, "%s: not enough space in the buffer (needed %zu, largest block available %zu)\n",
|
// the last block is our last resort
|
||||||
__func__, size, max_avail);
|
struct free_block * block = &alloc->free_blocks[alloc->n_free_blocks - 1];
|
||||||
GGML_ASSERT(!"not enough space in the buffer");
|
if (block->size >= size) {
|
||||||
|
best_fit_block = alloc->n_free_blocks - 1;
|
||||||
|
max_avail = MAX(max_avail, block->size);
|
||||||
|
} else {
|
||||||
|
fprintf(stderr, "%s: not enough space in the buffer (needed %zu, largest block available %zu)\n",
|
||||||
|
__func__, size, max_avail);
|
||||||
|
GGML_ASSERT(!"not enough space in the buffer");
|
||||||
return;
|
return;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
struct free_block * block = &alloc->free_blocks[best_fit_block];
|
struct free_block * block = &alloc->free_blocks[best_fit_block];
|
||||||
void * addr = block->addr;
|
void * addr = block->addr;
|
||||||
|
@ -229,6 +238,17 @@ static void ggml_allocator_free_tensor(struct ggml_allocr * alloc, struct ggml_t
|
||||||
alloc->n_free_blocks++;
|
alloc->n_free_blocks++;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void ggml_allocr_set_parse_seq(struct ggml_allocr * alloc, int * list, int n) {
|
||||||
|
int pos = 0;
|
||||||
|
for (int i = 0; i < n; i++) {
|
||||||
|
if (list[i] != -1) {
|
||||||
|
alloc->parse_seq[pos] = list[i];
|
||||||
|
pos++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
alloc->has_parse_seq = true;
|
||||||
|
}
|
||||||
|
|
||||||
void ggml_allocr_reset(struct ggml_allocr * alloc) {
|
void ggml_allocr_reset(struct ggml_allocr * alloc) {
|
||||||
alloc->n_free_blocks = 1;
|
alloc->n_free_blocks = 1;
|
||||||
size_t align_offset = aligned_offset(alloc->data, 0, alloc->alignment);
|
size_t align_offset = aligned_offset(alloc->data, 0, alloc->alignment);
|
||||||
|
@ -248,6 +268,8 @@ struct ggml_allocr * ggml_allocr_new(void * data, size_t size, size_t alignment)
|
||||||
/*.hash_table = */ {{0}},
|
/*.hash_table = */ {{0}},
|
||||||
/*.max_size = */ 0,
|
/*.max_size = */ 0,
|
||||||
/*.measure = */ false,
|
/*.measure = */ false,
|
||||||
|
/*.parse_seq = */ {0},
|
||||||
|
/*.has_parse_seq = */ false,
|
||||||
#ifdef GGML_ALLOCATOR_DEBUG
|
#ifdef GGML_ALLOCATOR_DEBUG
|
||||||
/*.allocated_tensors = */ = {0},
|
/*.allocated_tensors = */ = {0},
|
||||||
#endif
|
#endif
|
||||||
|
@ -275,6 +297,8 @@ struct ggml_allocr * ggml_allocr_new_measure(size_t alignment) {
|
||||||
/*.hash_table = */ {{0}},
|
/*.hash_table = */ {{0}},
|
||||||
/*.max_size = */ 0,
|
/*.max_size = */ 0,
|
||||||
/*.measure = */ true,
|
/*.measure = */ true,
|
||||||
|
/*.parse_seq = */ {0},
|
||||||
|
/*.has_parse_seq = */ false,
|
||||||
#ifdef GGML_ALLOCATOR_DEBUG
|
#ifdef GGML_ALLOCATOR_DEBUG
|
||||||
/*.allocated_tensors = */ = {0},
|
/*.allocated_tensors = */ = {0},
|
||||||
#endif
|
#endif
|
||||||
|
@ -473,7 +497,13 @@ static size_t ggml_allocator_alloc_graph_tensors_n(
|
||||||
allocate_node(alloc, input);
|
allocate_node(alloc, input);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
for (int i = 0; i < gf->n_nodes; i++) {
|
for (int ind = 0; ind < gf->n_nodes; ind++) {
|
||||||
|
int i;
|
||||||
|
if (alloc->has_parse_seq) {
|
||||||
|
i = alloc->parse_seq[ind];
|
||||||
|
} else {
|
||||||
|
i = ind;
|
||||||
|
}
|
||||||
struct ggml_tensor * node = gf->nodes[i];
|
struct ggml_tensor * node = gf->nodes[i];
|
||||||
|
|
||||||
// allocate parents (leafs)
|
// allocate parents (leafs)
|
||||||
|
|
|
@ -10,6 +10,10 @@ extern "C" {
|
||||||
GGML_API struct ggml_allocr * ggml_allocr_new(void * data, size_t size, size_t alignment);
|
GGML_API struct ggml_allocr * ggml_allocr_new(void * data, size_t size, size_t alignment);
|
||||||
GGML_API struct ggml_allocr * ggml_allocr_new_measure(size_t alignment);
|
GGML_API struct ggml_allocr * ggml_allocr_new_measure(size_t alignment);
|
||||||
|
|
||||||
|
// tell the allocator to parse nodes following the order described in the list
|
||||||
|
// you should call this if your graph are optimized to execute out-of-order
|
||||||
|
GGML_API void ggml_allocr_set_parse_seq(struct ggml_allocr * alloc, int * list, int n);
|
||||||
|
|
||||||
GGML_API void ggml_allocr_free(struct ggml_allocr * alloc);
|
GGML_API void ggml_allocr_free(struct ggml_allocr * alloc);
|
||||||
GGML_API bool ggml_allocr_is_measure(struct ggml_allocr * alloc);
|
GGML_API bool ggml_allocr_is_measure(struct ggml_allocr * alloc);
|
||||||
GGML_API void ggml_allocr_reset(struct ggml_allocr * alloc);
|
GGML_API void ggml_allocr_reset(struct ggml_allocr * alloc);
|
||||||
|
|
12
ggml-cuda.cu
12
ggml-cuda.cu
|
@ -6465,3 +6465,15 @@ bool ggml_cuda_compute_forward(struct ggml_compute_params * params, struct ggml_
|
||||||
func(tensor->src[0], tensor->src[1], tensor);
|
func(tensor->src[0], tensor->src[1], tensor);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int ggml_cuda_get_device_count() {
|
||||||
|
int device_count;
|
||||||
|
CUDA_CHECK(cudaGetDeviceCount(&device_count));
|
||||||
|
return device_count;
|
||||||
|
}
|
||||||
|
|
||||||
|
void ggml_cuda_get_device_description(int device, char * description, size_t description_size) {
|
||||||
|
cudaDeviceProp prop;
|
||||||
|
CUDA_CHECK(cudaGetDeviceProperties(&prop, device));
|
||||||
|
snprintf(description, description_size, "%s", prop.name);
|
||||||
|
}
|
||||||
|
|
38
ggml-cuda.h
38
ggml-cuda.h
|
@ -8,29 +8,25 @@ extern "C" {
|
||||||
|
|
||||||
#define GGML_CUDA_MAX_DEVICES 16
|
#define GGML_CUDA_MAX_DEVICES 16
|
||||||
|
|
||||||
void ggml_init_cublas(void);
|
GGML_API void ggml_init_cublas(void);
|
||||||
void ggml_cuda_set_tensor_split(const float * tensor_split);
|
GGML_API void * ggml_cuda_host_malloc(size_t size);
|
||||||
|
GGML_API void ggml_cuda_host_free(void * ptr);
|
||||||
|
|
||||||
void ggml_cuda_mul(const struct ggml_tensor * src0, const struct ggml_tensor * src1, struct ggml_tensor * dst);
|
GGML_API bool ggml_cuda_can_mul_mat(const struct ggml_tensor * src0, const struct ggml_tensor * src1, struct ggml_tensor * dst);
|
||||||
bool ggml_cuda_can_mul_mat(const struct ggml_tensor * src0, const struct ggml_tensor * src1, struct ggml_tensor * dst);
|
GGML_API void ggml_cuda_set_tensor_split(const float * tensor_split);
|
||||||
size_t ggml_cuda_mul_mat_get_wsize(const struct ggml_tensor * src0, const struct ggml_tensor * src1, struct ggml_tensor * dst);
|
GGML_API void ggml_cuda_transform_tensor(void * data, struct ggml_tensor * tensor);
|
||||||
void ggml_cuda_mul_mat(const struct ggml_tensor * src0, const struct ggml_tensor * src1, struct ggml_tensor * dst, void * wdata, size_t wsize);
|
GGML_API void ggml_cuda_free_data(struct ggml_tensor * tensor);
|
||||||
|
GGML_API void ggml_cuda_assign_buffers(struct ggml_tensor * tensor);
|
||||||
|
GGML_API void ggml_cuda_assign_buffers_no_scratch(struct ggml_tensor * tensor);
|
||||||
|
GGML_API void ggml_cuda_assign_buffers_force_inplace(struct ggml_tensor * tensor);
|
||||||
|
GGML_API void ggml_cuda_set_main_device(int main_device);
|
||||||
|
GGML_API void ggml_cuda_set_mul_mat_q(bool mul_mat_q);
|
||||||
|
GGML_API void ggml_cuda_set_scratch_size(size_t scratch_size);
|
||||||
|
GGML_API void ggml_cuda_free_scratch(void);
|
||||||
|
GGML_API bool ggml_cuda_compute_forward(struct ggml_compute_params * params, struct ggml_tensor * tensor);
|
||||||
|
|
||||||
// TODO: export these with GGML_API
|
GGML_API int ggml_cuda_get_device_count(void);
|
||||||
void * ggml_cuda_host_malloc(size_t size);
|
GGML_API void ggml_cuda_get_device_description(int device, char * description, size_t description_size);
|
||||||
void ggml_cuda_host_free(void * ptr);
|
|
||||||
|
|
||||||
void ggml_cuda_transform_tensor(void * data, struct ggml_tensor * tensor);
|
|
||||||
|
|
||||||
void ggml_cuda_free_data(struct ggml_tensor * tensor);
|
|
||||||
void ggml_cuda_assign_buffers(struct ggml_tensor * tensor);
|
|
||||||
void ggml_cuda_assign_buffers_no_scratch(struct ggml_tensor * tensor);
|
|
||||||
void ggml_cuda_assign_buffers_force_inplace(struct ggml_tensor * tensor);
|
|
||||||
void ggml_cuda_set_main_device(int main_device);
|
|
||||||
void ggml_cuda_set_mul_mat_q(bool mul_mat_q);
|
|
||||||
void ggml_cuda_set_scratch_size(size_t scratch_size);
|
|
||||||
void ggml_cuda_free_scratch(void);
|
|
||||||
bool ggml_cuda_compute_forward(struct ggml_compute_params * params, struct ggml_tensor * tensor);
|
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
|
|
|
@ -63,10 +63,13 @@ void ggml_metal_get_tensor(struct ggml_metal_context * ctx, struct ggml_tensor *
|
||||||
|
|
||||||
// try to find operations that can be run concurrently in the graph
|
// try to find operations that can be run concurrently in the graph
|
||||||
// you should run it again if the topology of your graph changes
|
// you should run it again if the topology of your graph changes
|
||||||
void ggml_metal_graph_find_concurrency(struct ggml_metal_context * ctx, struct ggml_cgraph * gf);
|
void ggml_metal_graph_find_concurrency(struct ggml_metal_context * ctx, struct ggml_cgraph * gf, bool check_mem);
|
||||||
|
|
||||||
// if the graph has been optimized for concurrently dispatch
|
// if the graph has been optimized for concurrently dispatch, return length of the concur_list if optimized
|
||||||
bool ggml_metal_if_optimized(struct ggml_metal_context * ctx);
|
int ggml_metal_if_optimized(struct ggml_metal_context * ctx);
|
||||||
|
|
||||||
|
// output the concur_list for ggml_alloc
|
||||||
|
int * ggml_metal_get_concur_list(struct ggml_metal_context * ctx);
|
||||||
|
|
||||||
// same as ggml_graph_compute but uses Metal
|
// same as ggml_graph_compute but uses Metal
|
||||||
// creates gf->n_threads command buffers in parallel
|
// creates gf->n_threads command buffers in parallel
|
||||||
|
|
195
ggml-metal.m
195
ggml-metal.m
|
@ -5,7 +5,6 @@
|
||||||
#import <Foundation/Foundation.h>
|
#import <Foundation/Foundation.h>
|
||||||
|
|
||||||
#import <Metal/Metal.h>
|
#import <Metal/Metal.h>
|
||||||
#import <MetalPerformanceShaders/MetalPerformanceShaders.h>
|
|
||||||
|
|
||||||
#undef MIN
|
#undef MIN
|
||||||
#undef MAX
|
#undef MAX
|
||||||
|
@ -79,6 +78,14 @@ struct ggml_metal_context {
|
||||||
GGML_METAL_DECL_KERNEL(mul_mat_q4_K_f32);
|
GGML_METAL_DECL_KERNEL(mul_mat_q4_K_f32);
|
||||||
GGML_METAL_DECL_KERNEL(mul_mat_q5_K_f32);
|
GGML_METAL_DECL_KERNEL(mul_mat_q5_K_f32);
|
||||||
GGML_METAL_DECL_KERNEL(mul_mat_q6_K_f32);
|
GGML_METAL_DECL_KERNEL(mul_mat_q6_K_f32);
|
||||||
|
GGML_METAL_DECL_KERNEL(mul_mm_f16_f32);
|
||||||
|
GGML_METAL_DECL_KERNEL(mul_mm_q4_0_f32);
|
||||||
|
GGML_METAL_DECL_KERNEL(mul_mm_q4_1_f32);
|
||||||
|
GGML_METAL_DECL_KERNEL(mul_mm_q2_K_f32);
|
||||||
|
GGML_METAL_DECL_KERNEL(mul_mm_q3_K_f32);
|
||||||
|
GGML_METAL_DECL_KERNEL(mul_mm_q4_K_f32);
|
||||||
|
GGML_METAL_DECL_KERNEL(mul_mm_q5_K_f32);
|
||||||
|
GGML_METAL_DECL_KERNEL(mul_mm_q6_K_f32);
|
||||||
GGML_METAL_DECL_KERNEL(rope);
|
GGML_METAL_DECL_KERNEL(rope);
|
||||||
GGML_METAL_DECL_KERNEL(alibi_f32);
|
GGML_METAL_DECL_KERNEL(alibi_f32);
|
||||||
GGML_METAL_DECL_KERNEL(cpy_f32_f16);
|
GGML_METAL_DECL_KERNEL(cpy_f32_f16);
|
||||||
|
@ -110,13 +117,6 @@ struct ggml_metal_context * ggml_metal_init(int n_cb) {
|
||||||
ctx->n_buffers = 0;
|
ctx->n_buffers = 0;
|
||||||
ctx->concur_list_len = 0;
|
ctx->concur_list_len = 0;
|
||||||
|
|
||||||
// determine if we can use MPS
|
|
||||||
if (MPSSupportsMTLDevice(ctx->device)) {
|
|
||||||
fprintf(stderr, "%s: using MPS\n", __func__);
|
|
||||||
} else {
|
|
||||||
fprintf(stderr, "%s: not using MPS\n", __func__);
|
|
||||||
GGML_ASSERT(false && "MPS not supported");
|
|
||||||
}
|
|
||||||
|
|
||||||
#if 0
|
#if 0
|
||||||
// compile from source string and show compile log
|
// compile from source string and show compile log
|
||||||
|
@ -163,10 +163,15 @@ struct ggml_metal_context * ggml_metal_init(int n_cb) {
|
||||||
|
|
||||||
// load kernels
|
// load kernels
|
||||||
{
|
{
|
||||||
|
NSError * error = nil;
|
||||||
#define GGML_METAL_ADD_KERNEL(name) \
|
#define GGML_METAL_ADD_KERNEL(name) \
|
||||||
ctx->function_##name = [ctx->library newFunctionWithName:@"kernel_"#name]; \
|
ctx->function_##name = [ctx->library newFunctionWithName:@"kernel_"#name]; \
|
||||||
ctx->pipeline_##name = [ctx->device newComputePipelineStateWithFunction:ctx->function_##name error:nil]; \
|
ctx->pipeline_##name = [ctx->device newComputePipelineStateWithFunction:ctx->function_##name error:&error]; \
|
||||||
fprintf(stderr, "%s: loaded %-32s %16p\n", __func__, "kernel_"#name, (void *) ctx->pipeline_##name);
|
fprintf(stderr, "%s: loaded %-32s %16p\n", __func__, "kernel_"#name, (void *) ctx->pipeline_##name); \
|
||||||
|
if (error) { \
|
||||||
|
fprintf(stderr, "%s: load pipeline error: %s\n", __func__, [[error description] UTF8String]); \
|
||||||
|
return NULL; \
|
||||||
|
}
|
||||||
|
|
||||||
GGML_METAL_ADD_KERNEL(add);
|
GGML_METAL_ADD_KERNEL(add);
|
||||||
GGML_METAL_ADD_KERNEL(add_row);
|
GGML_METAL_ADD_KERNEL(add_row);
|
||||||
|
@ -196,6 +201,14 @@ struct ggml_metal_context * ggml_metal_init(int n_cb) {
|
||||||
GGML_METAL_ADD_KERNEL(mul_mat_q4_K_f32);
|
GGML_METAL_ADD_KERNEL(mul_mat_q4_K_f32);
|
||||||
GGML_METAL_ADD_KERNEL(mul_mat_q5_K_f32);
|
GGML_METAL_ADD_KERNEL(mul_mat_q5_K_f32);
|
||||||
GGML_METAL_ADD_KERNEL(mul_mat_q6_K_f32);
|
GGML_METAL_ADD_KERNEL(mul_mat_q6_K_f32);
|
||||||
|
GGML_METAL_ADD_KERNEL(mul_mm_f16_f32);
|
||||||
|
GGML_METAL_ADD_KERNEL(mul_mm_q4_0_f32);
|
||||||
|
GGML_METAL_ADD_KERNEL(mul_mm_q4_1_f32);
|
||||||
|
GGML_METAL_ADD_KERNEL(mul_mm_q2_K_f32);
|
||||||
|
GGML_METAL_ADD_KERNEL(mul_mm_q3_K_f32);
|
||||||
|
GGML_METAL_ADD_KERNEL(mul_mm_q4_K_f32);
|
||||||
|
GGML_METAL_ADD_KERNEL(mul_mm_q5_K_f32);
|
||||||
|
GGML_METAL_ADD_KERNEL(mul_mm_q6_K_f32);
|
||||||
GGML_METAL_ADD_KERNEL(rope);
|
GGML_METAL_ADD_KERNEL(rope);
|
||||||
GGML_METAL_ADD_KERNEL(alibi_f32);
|
GGML_METAL_ADD_KERNEL(alibi_f32);
|
||||||
GGML_METAL_ADD_KERNEL(cpy_f32_f16);
|
GGML_METAL_ADD_KERNEL(cpy_f32_f16);
|
||||||
|
@ -228,11 +241,12 @@ void ggml_metal_set_n_cb(struct ggml_metal_context * ctx, int n_cb) {
|
||||||
ctx->n_cb = n_cb;
|
ctx->n_cb = n_cb;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool ggml_metal_if_optimized(struct ggml_metal_context * ctx) {
|
int ggml_metal_if_optimized(struct ggml_metal_context * ctx) {
|
||||||
if (ctx->concur_list_len) {
|
return ctx->concur_list_len;
|
||||||
return true;
|
}
|
||||||
}
|
|
||||||
return false;
|
int * ggml_metal_get_concur_list(struct ggml_metal_context * ctx) {
|
||||||
|
return ctx->concur_list;
|
||||||
}
|
}
|
||||||
|
|
||||||
// finds the Metal buffer that contains the tensor data on the GPU device
|
// finds the Metal buffer that contains the tensor data on the GPU device
|
||||||
|
@ -375,7 +389,7 @@ void ggml_metal_get_tensor(
|
||||||
|
|
||||||
void ggml_metal_graph_find_concurrency(
|
void ggml_metal_graph_find_concurrency(
|
||||||
struct ggml_metal_context * ctx,
|
struct ggml_metal_context * ctx,
|
||||||
struct ggml_cgraph * gf) {
|
struct ggml_cgraph * gf, bool check_mem) {
|
||||||
int search_depth = gf->n_nodes; //we only find concurrency in this range to avoid wasting too much time
|
int search_depth = gf->n_nodes; //we only find concurrency in this range to avoid wasting too much time
|
||||||
int nodes_unused[GGML_MAX_CONCUR];
|
int nodes_unused[GGML_MAX_CONCUR];
|
||||||
|
|
||||||
|
@ -422,7 +436,7 @@ void ggml_metal_graph_find_concurrency(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (exe_flag) {
|
if (exe_flag && check_mem) {
|
||||||
// check if nodes[i]'s data will be overwritten by a node before nodes[i].
|
// check if nodes[i]'s data will be overwritten by a node before nodes[i].
|
||||||
// if node[5] and node[3] write to the same memory region, then we can't issue node[5] before node[3]
|
// if node[5] and node[3] write to the same memory region, then we can't issue node[5] before node[3]
|
||||||
int64_t data_start = (int64_t) gf->nodes[i]->data;
|
int64_t data_start = (int64_t) gf->nodes[i]->data;
|
||||||
|
@ -506,7 +520,7 @@ void ggml_metal_graph_compute(
|
||||||
|
|
||||||
id<MTLCommandBuffer> command_buffer = command_buffers[cb_idx];
|
id<MTLCommandBuffer> command_buffer = command_buffers[cb_idx];
|
||||||
|
|
||||||
id<MTLComputeCommandEncoder> encoder = nil;
|
id<MTLComputeCommandEncoder> encoder = [command_buffer computeCommandEncoderWithDescriptor: edesc];
|
||||||
|
|
||||||
const int node_start = (cb_idx + 0) * n_nodes_per_cb;
|
const int node_start = (cb_idx + 0) * n_nodes_per_cb;
|
||||||
const int node_end = (cb_idx == n_cb - 1) ? n_nodes : (cb_idx + 1) * n_nodes_per_cb;
|
const int node_end = (cb_idx == n_cb - 1) ? n_nodes : (cb_idx + 1) * n_nodes_per_cb;
|
||||||
|
@ -515,10 +529,6 @@ void ggml_metal_graph_compute(
|
||||||
const int i = has_concur ? ctx->concur_list[ind] : ind;
|
const int i = has_concur ? ctx->concur_list[ind] : ind;
|
||||||
|
|
||||||
if (i == -1) {
|
if (i == -1) {
|
||||||
if (encoder == nil) {
|
|
||||||
encoder = [command_buffer computeCommandEncoderWithDescriptor: edesc];
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
[encoder memoryBarrierWithScope:MTLBarrierScopeBuffers];
|
[encoder memoryBarrierWithScope:MTLBarrierScopeBuffers];
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
@ -592,10 +602,6 @@ void ggml_metal_graph_compute(
|
||||||
} break;
|
} break;
|
||||||
case GGML_OP_ADD:
|
case GGML_OP_ADD:
|
||||||
{
|
{
|
||||||
if (encoder == nil) {
|
|
||||||
encoder = [command_buffer computeCommandEncoderWithDescriptor: edesc];
|
|
||||||
}
|
|
||||||
|
|
||||||
if (ggml_nelements(src1) == ne10) {
|
if (ggml_nelements(src1) == ne10) {
|
||||||
// src1 is a row
|
// src1 is a row
|
||||||
[encoder setComputePipelineState:ctx->pipeline_add_row];
|
[encoder setComputePipelineState:ctx->pipeline_add_row];
|
||||||
|
@ -613,10 +619,6 @@ void ggml_metal_graph_compute(
|
||||||
} break;
|
} break;
|
||||||
case GGML_OP_MUL:
|
case GGML_OP_MUL:
|
||||||
{
|
{
|
||||||
if (encoder == nil) {
|
|
||||||
encoder = [command_buffer computeCommandEncoderWithDescriptor: edesc];
|
|
||||||
}
|
|
||||||
|
|
||||||
if (ggml_nelements(src1) == ne10) {
|
if (ggml_nelements(src1) == ne10) {
|
||||||
// src1 is a row
|
// src1 is a row
|
||||||
[encoder setComputePipelineState:ctx->pipeline_mul_row];
|
[encoder setComputePipelineState:ctx->pipeline_mul_row];
|
||||||
|
@ -634,10 +636,6 @@ void ggml_metal_graph_compute(
|
||||||
} break;
|
} break;
|
||||||
case GGML_OP_SCALE:
|
case GGML_OP_SCALE:
|
||||||
{
|
{
|
||||||
if (encoder == nil) {
|
|
||||||
encoder = [command_buffer computeCommandEncoderWithDescriptor: edesc];
|
|
||||||
}
|
|
||||||
|
|
||||||
const float scale = *(const float *) src1->data;
|
const float scale = *(const float *) src1->data;
|
||||||
|
|
||||||
[encoder setComputePipelineState:ctx->pipeline_scale];
|
[encoder setComputePipelineState:ctx->pipeline_scale];
|
||||||
|
@ -653,10 +651,6 @@ void ggml_metal_graph_compute(
|
||||||
switch (ggml_get_unary_op(gf->nodes[i])) {
|
switch (ggml_get_unary_op(gf->nodes[i])) {
|
||||||
case GGML_UNARY_OP_SILU:
|
case GGML_UNARY_OP_SILU:
|
||||||
{
|
{
|
||||||
if (encoder == nil) {
|
|
||||||
encoder = [command_buffer computeCommandEncoderWithDescriptor: edesc];
|
|
||||||
}
|
|
||||||
|
|
||||||
[encoder setComputePipelineState:ctx->pipeline_silu];
|
[encoder setComputePipelineState:ctx->pipeline_silu];
|
||||||
[encoder setBuffer:id_src0 offset:offs_src0 atIndex:0];
|
[encoder setBuffer:id_src0 offset:offs_src0 atIndex:0];
|
||||||
[encoder setBuffer:id_dst offset:offs_dst atIndex:1];
|
[encoder setBuffer:id_dst offset:offs_dst atIndex:1];
|
||||||
|
@ -667,10 +661,6 @@ void ggml_metal_graph_compute(
|
||||||
} break;
|
} break;
|
||||||
case GGML_UNARY_OP_RELU:
|
case GGML_UNARY_OP_RELU:
|
||||||
{
|
{
|
||||||
if (encoder == nil) {
|
|
||||||
encoder = [command_buffer computeCommandEncoderWithDescriptor: edesc];
|
|
||||||
}
|
|
||||||
|
|
||||||
[encoder setComputePipelineState:ctx->pipeline_relu];
|
[encoder setComputePipelineState:ctx->pipeline_relu];
|
||||||
[encoder setBuffer:id_src0 offset:offs_src0 atIndex:0];
|
[encoder setBuffer:id_src0 offset:offs_src0 atIndex:0];
|
||||||
[encoder setBuffer:id_dst offset:offs_dst atIndex:1];
|
[encoder setBuffer:id_dst offset:offs_dst atIndex:1];
|
||||||
|
@ -681,10 +671,6 @@ void ggml_metal_graph_compute(
|
||||||
} break;
|
} break;
|
||||||
case GGML_UNARY_OP_GELU:
|
case GGML_UNARY_OP_GELU:
|
||||||
{
|
{
|
||||||
if (encoder == nil) {
|
|
||||||
encoder = [command_buffer computeCommandEncoderWithDescriptor: edesc];
|
|
||||||
}
|
|
||||||
|
|
||||||
[encoder setComputePipelineState:ctx->pipeline_gelu];
|
[encoder setComputePipelineState:ctx->pipeline_gelu];
|
||||||
[encoder setBuffer:id_src0 offset:offs_src0 atIndex:0];
|
[encoder setBuffer:id_src0 offset:offs_src0 atIndex:0];
|
||||||
[encoder setBuffer:id_dst offset:offs_dst atIndex:1];
|
[encoder setBuffer:id_dst offset:offs_dst atIndex:1];
|
||||||
|
@ -701,10 +687,6 @@ void ggml_metal_graph_compute(
|
||||||
} break;
|
} break;
|
||||||
case GGML_OP_SOFT_MAX:
|
case GGML_OP_SOFT_MAX:
|
||||||
{
|
{
|
||||||
if (encoder == nil) {
|
|
||||||
encoder = [command_buffer computeCommandEncoderWithDescriptor: edesc];
|
|
||||||
}
|
|
||||||
|
|
||||||
const int nth = 32;
|
const int nth = 32;
|
||||||
|
|
||||||
[encoder setComputePipelineState:ctx->pipeline_soft_max];
|
[encoder setComputePipelineState:ctx->pipeline_soft_max];
|
||||||
|
@ -719,10 +701,6 @@ void ggml_metal_graph_compute(
|
||||||
} break;
|
} break;
|
||||||
case GGML_OP_DIAG_MASK_INF:
|
case GGML_OP_DIAG_MASK_INF:
|
||||||
{
|
{
|
||||||
if (encoder == nil) {
|
|
||||||
encoder = [command_buffer computeCommandEncoderWithDescriptor: edesc];
|
|
||||||
}
|
|
||||||
|
|
||||||
const int n_past = ((int32_t *)(dst->op_params))[0];
|
const int n_past = ((int32_t *)(dst->op_params))[0];
|
||||||
|
|
||||||
[encoder setComputePipelineState:ctx->pipeline_diag_mask_inf];
|
[encoder setComputePipelineState:ctx->pipeline_diag_mask_inf];
|
||||||
|
@ -740,53 +718,43 @@ void ggml_metal_graph_compute(
|
||||||
|
|
||||||
GGML_ASSERT(ne00 == ne10);
|
GGML_ASSERT(ne00 == ne10);
|
||||||
// GGML_ASSERT(ne02 == ne12); // Should be checked on individual data types until broadcast is implemented everywhere
|
// GGML_ASSERT(ne02 == ne12); // Should be checked on individual data types until broadcast is implemented everywhere
|
||||||
|
uint gqa = ne12/ne02;
|
||||||
GGML_ASSERT(ne03 == ne13);
|
GGML_ASSERT(ne03 == ne13);
|
||||||
|
|
||||||
|
// for now the matrix-matrix multiplication kernel only works on A14+/M1+ SoCs
|
||||||
|
// AMD GPU and older A-chips will reuse matrix-vector multiplication kernel
|
||||||
if (ggml_is_contiguous(src0) &&
|
if (ggml_is_contiguous(src0) &&
|
||||||
ggml_is_contiguous(src1) &&
|
ggml_is_contiguous(src1) &&
|
||||||
(src0t == GGML_TYPE_F32 || src0t == GGML_TYPE_F16) && ne11 > 1) {
|
src1t == GGML_TYPE_F32 &&
|
||||||
|
[ctx->device supportsFamily:MTLGPUFamilyApple7] &&
|
||||||
if (encoder != nil) {
|
ne00%32 == 0 &&
|
||||||
[encoder endEncoding];
|
ne11 > 1) {
|
||||||
encoder = nil;
|
switch (src0->type) {
|
||||||
|
case GGML_TYPE_F16: [encoder setComputePipelineState:ctx->pipeline_mul_mm_f16_f32]; break;
|
||||||
|
case GGML_TYPE_Q4_0: [encoder setComputePipelineState:ctx->pipeline_mul_mm_q4_0_f32]; break;
|
||||||
|
case GGML_TYPE_Q4_1: [encoder setComputePipelineState:ctx->pipeline_mul_mm_q4_1_f32]; break;
|
||||||
|
case GGML_TYPE_Q2_K: [encoder setComputePipelineState:ctx->pipeline_mul_mm_q2_K_f32]; break;
|
||||||
|
case GGML_TYPE_Q3_K: [encoder setComputePipelineState:ctx->pipeline_mul_mm_q3_K_f32]; break;
|
||||||
|
case GGML_TYPE_Q4_K: [encoder setComputePipelineState:ctx->pipeline_mul_mm_q4_K_f32]; break;
|
||||||
|
case GGML_TYPE_Q5_K: [encoder setComputePipelineState:ctx->pipeline_mul_mm_q5_K_f32]; break;
|
||||||
|
case GGML_TYPE_Q6_K: [encoder setComputePipelineState:ctx->pipeline_mul_mm_q6_K_f32]; break;
|
||||||
|
default: GGML_ASSERT(false && "MUL MAT-MAT not implemented");
|
||||||
|
}
|
||||||
|
[encoder setBuffer:id_src0 offset:offs_src0 atIndex:0];
|
||||||
|
[encoder setBuffer:id_src1 offset:offs_src1 atIndex:1];
|
||||||
|
[encoder setBuffer:id_dst offset:offs_dst atIndex:2];
|
||||||
|
[encoder setBytes:&ne00 length:sizeof(ne00) atIndex:3];
|
||||||
|
[encoder setBytes:&ne02 length:sizeof(ne02) atIndex:4];
|
||||||
|
[encoder setBytes:&nb01 length:sizeof(nb01) atIndex:5];
|
||||||
|
[encoder setBytes:&nb02 length:sizeof(nb02) atIndex:6];
|
||||||
|
[encoder setBytes:&ne12 length:sizeof(ne12) atIndex:7];
|
||||||
|
[encoder setBytes:&ne0 length:sizeof(ne0) atIndex:8];
|
||||||
|
[encoder setBytes:&ne1 length:sizeof(ne1) atIndex:9];
|
||||||
|
[encoder setBytes:&gqa length:sizeof(gqa) atIndex:10];
|
||||||
|
[encoder setThreadgroupMemoryLength:8192 atIndex:0];
|
||||||
|
[encoder dispatchThreadgroups:MTLSizeMake( (ne11+31)/32, (ne01+63) / 64, ne12) threadsPerThreadgroup:MTLSizeMake(128, 1, 1)];
|
||||||
}
|
}
|
||||||
|
else {
|
||||||
MPSDataType src0dt = src0t == GGML_TYPE_F32 ? MPSDataTypeFloat32 : MPSDataTypeFloat16;
|
|
||||||
MPSDataType src1dt = src1t == GGML_TYPE_F32 ? MPSDataTypeFloat32 : MPSDataTypeFloat16;
|
|
||||||
|
|
||||||
// for F32 x F32 we use MPS
|
|
||||||
MPSMatrixDescriptor * desc0 = [MPSMatrixDescriptor
|
|
||||||
matrixDescriptorWithRows:ne01 columns:ne00 rowBytes:src0->nb[1] dataType:src0dt];
|
|
||||||
|
|
||||||
MPSMatrixDescriptor * desc1 = [MPSMatrixDescriptor
|
|
||||||
matrixDescriptorWithRows:ne11 columns:ne10 rowBytes:src1->nb[1] dataType:src1dt];
|
|
||||||
|
|
||||||
MPSMatrixDescriptor * desc = [MPSMatrixDescriptor
|
|
||||||
matrixDescriptorWithRows:ne1 columns:ne0 rowBytes:dst->nb[1] dataType:MPSDataTypeFloat32];
|
|
||||||
|
|
||||||
MPSMatrixMultiplication * mul = [[MPSMatrixMultiplication alloc]
|
|
||||||
initWithDevice:ctx->device transposeLeft:false transposeRight:true
|
|
||||||
resultRows:ne11 resultColumns:ne01 interiorColumns:ne00 alpha:1.0 beta:0.0];
|
|
||||||
|
|
||||||
// we need to do ne12 multiplications
|
|
||||||
// TODO: is there a way to do this in parallel - currently very slow ..
|
|
||||||
// TODO: might be possible to offload part of the computation to ANE using Accelerate's CBLAS
|
|
||||||
for (int64_t i02 = 0; i02 < ne12; ++i02) {
|
|
||||||
size_t offs_src0_cur = offs_src0 + i02/(ne12/ne02)*nb02; // gqa not used for now
|
|
||||||
size_t offs_src1_cur = offs_src1 + i02*nb12;
|
|
||||||
size_t offs_dst_cur = offs_dst + i02*nb2;
|
|
||||||
|
|
||||||
MPSMatrix * mat_src0 = [[MPSMatrix alloc] initWithBuffer:id_src0 offset:offs_src0_cur descriptor:desc0];
|
|
||||||
MPSMatrix * mat_src1 = [[MPSMatrix alloc] initWithBuffer:id_src1 offset:offs_src1_cur descriptor:desc1];
|
|
||||||
MPSMatrix * mat_dst = [[MPSMatrix alloc] initWithBuffer:id_dst offset:offs_dst_cur descriptor:desc ];
|
|
||||||
|
|
||||||
[mul encodeToCommandBuffer:command_buffer leftMatrix:mat_src1 rightMatrix:mat_src0 resultMatrix:mat_dst];
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
if (encoder == nil) {
|
|
||||||
encoder = [command_buffer computeCommandEncoderWithDescriptor: edesc];
|
|
||||||
}
|
|
||||||
|
|
||||||
int nth0 = 32;
|
int nth0 = 32;
|
||||||
int nth1 = 1;
|
int nth1 = 1;
|
||||||
|
|
||||||
|
@ -885,23 +853,24 @@ void ggml_metal_graph_compute(
|
||||||
[encoder setBytes:&nb12 length:sizeof(nb12) atIndex:14];
|
[encoder setBytes:&nb12 length:sizeof(nb12) atIndex:14];
|
||||||
[encoder setBytes:&ne0 length:sizeof(ne0) atIndex:15];
|
[encoder setBytes:&ne0 length:sizeof(ne0) atIndex:15];
|
||||||
[encoder setBytes:&ne1 length:sizeof(ne1) atIndex:16];
|
[encoder setBytes:&ne1 length:sizeof(ne1) atIndex:16];
|
||||||
|
[encoder setBytes:&gqa length:sizeof(gqa) atIndex:17];
|
||||||
|
|
||||||
if (src0t == GGML_TYPE_Q4_0 || src0t == GGML_TYPE_Q4_1 ||
|
if (src0t == GGML_TYPE_Q4_0 || src0t == GGML_TYPE_Q4_1 ||
|
||||||
src0t == GGML_TYPE_Q2_K || src0t == GGML_TYPE_Q4_K) {
|
src0t == GGML_TYPE_Q2_K || src0t == GGML_TYPE_Q4_K) {
|
||||||
[encoder dispatchThreadgroups:MTLSizeMake((ne01 + 7) / 8, ne11, 1) threadsPerThreadgroup:MTLSizeMake(nth0, nth1, 1)];
|
[encoder dispatchThreadgroups:MTLSizeMake((ne01 + 7) / 8, ne11, ne12) threadsPerThreadgroup:MTLSizeMake(nth0, nth1, 1)];
|
||||||
}
|
}
|
||||||
else if (src0t == GGML_TYPE_Q3_K) {
|
else if (src0t == GGML_TYPE_Q3_K) {
|
||||||
#ifdef GGML_QKK_64
|
#ifdef GGML_QKK_64
|
||||||
[encoder dispatchThreadgroups:MTLSizeMake((ne01+1)/2, ne11, 1) threadsPerThreadgroup:MTLSizeMake(nth0, nth1, 1)];
|
[encoder dispatchThreadgroups:MTLSizeMake((ne01+1)/2, ne11, ne12) threadsPerThreadgroup:MTLSizeMake(nth0, nth1, 1)];
|
||||||
#else
|
#else
|
||||||
[encoder dispatchThreadgroups:MTLSizeMake((ne01+3)/4, ne11, 1) threadsPerThreadgroup:MTLSizeMake(nth0, nth1, 1)];
|
[encoder dispatchThreadgroups:MTLSizeMake((ne01+3)/4, ne11, ne12) threadsPerThreadgroup:MTLSizeMake(nth0, nth1, 1)];
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
else if (src0t == GGML_TYPE_Q5_K) {
|
else if (src0t == GGML_TYPE_Q5_K) {
|
||||||
[encoder dispatchThreadgroups:MTLSizeMake((ne01 + 3) / 4, ne11, 1) threadsPerThreadgroup:MTLSizeMake(nth0, nth1, 1)];
|
[encoder dispatchThreadgroups:MTLSizeMake((ne01 + 3) / 4, ne11, ne12) threadsPerThreadgroup:MTLSizeMake(nth0, nth1, 1)];
|
||||||
}
|
}
|
||||||
else if (src0t == GGML_TYPE_Q6_K) {
|
else if (src0t == GGML_TYPE_Q6_K) {
|
||||||
[encoder dispatchThreadgroups:MTLSizeMake((ne01+1)/2, ne11, 1) threadsPerThreadgroup:MTLSizeMake(nth0, nth1, 1)];
|
[encoder dispatchThreadgroups:MTLSizeMake((ne01+1)/2, ne11, ne12) threadsPerThreadgroup:MTLSizeMake(nth0, nth1, 1)];
|
||||||
} else {
|
} else {
|
||||||
[encoder setThreadgroupMemoryLength:nth0*sizeof(float) atIndex:0];
|
[encoder setThreadgroupMemoryLength:nth0*sizeof(float) atIndex:0];
|
||||||
[encoder dispatchThreadgroups:MTLSizeMake(ne01, ne11, ne12) threadsPerThreadgroup:MTLSizeMake(nth0, nth1, 1)];
|
[encoder dispatchThreadgroups:MTLSizeMake(ne01, ne11, ne12) threadsPerThreadgroup:MTLSizeMake(nth0, nth1, 1)];
|
||||||
|
@ -910,10 +879,6 @@ void ggml_metal_graph_compute(
|
||||||
} break;
|
} break;
|
||||||
case GGML_OP_GET_ROWS:
|
case GGML_OP_GET_ROWS:
|
||||||
{
|
{
|
||||||
if (encoder == nil) {
|
|
||||||
encoder = [command_buffer computeCommandEncoderWithDescriptor: edesc];
|
|
||||||
}
|
|
||||||
|
|
||||||
switch (src0->type) {
|
switch (src0->type) {
|
||||||
case GGML_TYPE_F16: [encoder setComputePipelineState:ctx->pipeline_get_rows_f16]; break;
|
case GGML_TYPE_F16: [encoder setComputePipelineState:ctx->pipeline_get_rows_f16]; break;
|
||||||
case GGML_TYPE_Q4_0: [encoder setComputePipelineState:ctx->pipeline_get_rows_q4_0]; break;
|
case GGML_TYPE_Q4_0: [encoder setComputePipelineState:ctx->pipeline_get_rows_q4_0]; break;
|
||||||
|
@ -939,10 +904,6 @@ void ggml_metal_graph_compute(
|
||||||
} break;
|
} break;
|
||||||
case GGML_OP_RMS_NORM:
|
case GGML_OP_RMS_NORM:
|
||||||
{
|
{
|
||||||
if (encoder == nil) {
|
|
||||||
encoder = [command_buffer computeCommandEncoderWithDescriptor: edesc];
|
|
||||||
}
|
|
||||||
|
|
||||||
float eps;
|
float eps;
|
||||||
memcpy(&eps, dst->op_params, sizeof(float));
|
memcpy(&eps, dst->op_params, sizeof(float));
|
||||||
|
|
||||||
|
@ -962,10 +923,6 @@ void ggml_metal_graph_compute(
|
||||||
} break;
|
} break;
|
||||||
case GGML_OP_NORM:
|
case GGML_OP_NORM:
|
||||||
{
|
{
|
||||||
if (encoder == nil) {
|
|
||||||
encoder = [command_buffer computeCommandEncoderWithDescriptor: edesc];
|
|
||||||
}
|
|
||||||
|
|
||||||
const float eps = 1e-5f;
|
const float eps = 1e-5f;
|
||||||
|
|
||||||
const int nth = 256;
|
const int nth = 256;
|
||||||
|
@ -984,10 +941,6 @@ void ggml_metal_graph_compute(
|
||||||
} break;
|
} break;
|
||||||
case GGML_OP_ALIBI:
|
case GGML_OP_ALIBI:
|
||||||
{
|
{
|
||||||
if (encoder == nil) {
|
|
||||||
encoder = [command_buffer computeCommandEncoderWithDescriptor: edesc];
|
|
||||||
}
|
|
||||||
|
|
||||||
GGML_ASSERT((src0t == GGML_TYPE_F32));
|
GGML_ASSERT((src0t == GGML_TYPE_F32));
|
||||||
|
|
||||||
const int n_past = ((int32_t *) dst->op_params)[0]; UNUSED(n_past);
|
const int n_past = ((int32_t *) dst->op_params)[0]; UNUSED(n_past);
|
||||||
|
@ -1027,10 +980,6 @@ void ggml_metal_graph_compute(
|
||||||
} break;
|
} break;
|
||||||
case GGML_OP_ROPE:
|
case GGML_OP_ROPE:
|
||||||
{
|
{
|
||||||
if (encoder == nil) {
|
|
||||||
encoder = [command_buffer computeCommandEncoderWithDescriptor: edesc];
|
|
||||||
}
|
|
||||||
|
|
||||||
const int n_past = ((int32_t *) dst->op_params)[0];
|
const int n_past = ((int32_t *) dst->op_params)[0];
|
||||||
const int n_dims = ((int32_t *) dst->op_params)[1];
|
const int n_dims = ((int32_t *) dst->op_params)[1];
|
||||||
const int mode = ((int32_t *) dst->op_params)[2];
|
const int mode = ((int32_t *) dst->op_params)[2];
|
||||||
|
@ -1071,10 +1020,6 @@ void ggml_metal_graph_compute(
|
||||||
case GGML_OP_CPY:
|
case GGML_OP_CPY:
|
||||||
case GGML_OP_CONT:
|
case GGML_OP_CONT:
|
||||||
{
|
{
|
||||||
if (encoder == nil) {
|
|
||||||
encoder = [command_buffer computeCommandEncoderWithDescriptor: edesc];
|
|
||||||
}
|
|
||||||
|
|
||||||
const int nth = 32;
|
const int nth = 32;
|
||||||
|
|
||||||
switch (src0t) {
|
switch (src0t) {
|
||||||
|
|
970
ggml-metal.metal
970
ggml-metal.metal
File diff suppressed because it is too large
Load diff
245
ggml.c
245
ggml.c
|
@ -1644,11 +1644,37 @@ static void ggml_vec_dot_q5_1_q8_1(const int n, float * restrict s, const void *
|
||||||
static void ggml_vec_dot_q8_0_q8_0(const int n, float * restrict s, const void * restrict vx, const void * restrict vy);
|
static void ggml_vec_dot_q8_0_q8_0(const int n, float * restrict s, const void * restrict vx, const void * restrict vy);
|
||||||
|
|
||||||
static const ggml_type_traits_t type_traits[GGML_TYPE_COUNT] = {
|
static const ggml_type_traits_t type_traits[GGML_TYPE_COUNT] = {
|
||||||
|
[GGML_TYPE_I8] = {
|
||||||
|
.type_name = "i8",
|
||||||
|
.blck_size = 1,
|
||||||
|
.type_size = sizeof(int8_t),
|
||||||
|
.is_quantized = false,
|
||||||
|
},
|
||||||
|
[GGML_TYPE_I16] = {
|
||||||
|
.type_name = "i16",
|
||||||
|
.blck_size = 1,
|
||||||
|
.type_size = sizeof(int16_t),
|
||||||
|
.is_quantized = false,
|
||||||
|
},
|
||||||
|
[GGML_TYPE_I32] = {
|
||||||
|
.type_name = "i32",
|
||||||
|
.blck_size = 1,
|
||||||
|
.type_size = sizeof(int32_t),
|
||||||
|
.is_quantized = false,
|
||||||
|
},
|
||||||
[GGML_TYPE_F32] = {
|
[GGML_TYPE_F32] = {
|
||||||
|
.type_name = "f32",
|
||||||
|
.blck_size = 1,
|
||||||
|
.type_size = sizeof(float),
|
||||||
|
.is_quantized = false,
|
||||||
.vec_dot = (ggml_vec_dot_t) ggml_vec_dot_f32,
|
.vec_dot = (ggml_vec_dot_t) ggml_vec_dot_f32,
|
||||||
.vec_dot_type = GGML_TYPE_F32,
|
.vec_dot_type = GGML_TYPE_F32,
|
||||||
},
|
},
|
||||||
[GGML_TYPE_F16] = {
|
[GGML_TYPE_F16] = {
|
||||||
|
.type_name = "f16",
|
||||||
|
.blck_size = 1,
|
||||||
|
.type_size = sizeof(ggml_fp16_t),
|
||||||
|
.is_quantized = false,
|
||||||
.to_float = (ggml_to_float_t) ggml_fp16_to_fp32_row,
|
.to_float = (ggml_to_float_t) ggml_fp16_to_fp32_row,
|
||||||
.from_float = (ggml_from_float_t) ggml_fp32_to_fp16_row,
|
.from_float = (ggml_from_float_t) ggml_fp32_to_fp16_row,
|
||||||
.from_float_reference = (ggml_from_float_t) ggml_fp32_to_fp16_row,
|
.from_float_reference = (ggml_from_float_t) ggml_fp32_to_fp16_row,
|
||||||
|
@ -1656,6 +1682,10 @@ static const ggml_type_traits_t type_traits[GGML_TYPE_COUNT] = {
|
||||||
.vec_dot_type = GGML_TYPE_F16,
|
.vec_dot_type = GGML_TYPE_F16,
|
||||||
},
|
},
|
||||||
[GGML_TYPE_Q4_0] = {
|
[GGML_TYPE_Q4_0] = {
|
||||||
|
.type_name = "q4_0",
|
||||||
|
.blck_size = QK4_0,
|
||||||
|
.type_size = sizeof(block_q4_0),
|
||||||
|
.is_quantized = true,
|
||||||
.to_float = (ggml_to_float_t) dequantize_row_q4_0,
|
.to_float = (ggml_to_float_t) dequantize_row_q4_0,
|
||||||
.from_float = quantize_row_q4_0,
|
.from_float = quantize_row_q4_0,
|
||||||
.from_float_reference = (ggml_from_float_t) quantize_row_q4_0_reference,
|
.from_float_reference = (ggml_from_float_t) quantize_row_q4_0_reference,
|
||||||
|
@ -1663,6 +1693,10 @@ static const ggml_type_traits_t type_traits[GGML_TYPE_COUNT] = {
|
||||||
.vec_dot_type = GGML_TYPE_Q8_0,
|
.vec_dot_type = GGML_TYPE_Q8_0,
|
||||||
},
|
},
|
||||||
[GGML_TYPE_Q4_1] = {
|
[GGML_TYPE_Q4_1] = {
|
||||||
|
.type_name = "q4_1",
|
||||||
|
.blck_size = QK4_1,
|
||||||
|
.type_size = sizeof(block_q4_1),
|
||||||
|
.is_quantized = true,
|
||||||
.to_float = (ggml_to_float_t) dequantize_row_q4_1,
|
.to_float = (ggml_to_float_t) dequantize_row_q4_1,
|
||||||
.from_float = quantize_row_q4_1,
|
.from_float = quantize_row_q4_1,
|
||||||
.from_float_reference = (ggml_from_float_t) quantize_row_q4_1_reference,
|
.from_float_reference = (ggml_from_float_t) quantize_row_q4_1_reference,
|
||||||
|
@ -1670,6 +1704,10 @@ static const ggml_type_traits_t type_traits[GGML_TYPE_COUNT] = {
|
||||||
.vec_dot_type = GGML_TYPE_Q8_1,
|
.vec_dot_type = GGML_TYPE_Q8_1,
|
||||||
},
|
},
|
||||||
[GGML_TYPE_Q5_0] = {
|
[GGML_TYPE_Q5_0] = {
|
||||||
|
.type_name = "q5_0",
|
||||||
|
.blck_size = QK5_0,
|
||||||
|
.type_size = sizeof(block_q5_0),
|
||||||
|
.is_quantized = true,
|
||||||
.to_float = (ggml_to_float_t) dequantize_row_q5_0,
|
.to_float = (ggml_to_float_t) dequantize_row_q5_0,
|
||||||
.from_float = quantize_row_q5_0,
|
.from_float = quantize_row_q5_0,
|
||||||
.from_float_reference = (ggml_from_float_t) quantize_row_q5_0_reference,
|
.from_float_reference = (ggml_from_float_t) quantize_row_q5_0_reference,
|
||||||
|
@ -1677,6 +1715,10 @@ static const ggml_type_traits_t type_traits[GGML_TYPE_COUNT] = {
|
||||||
.vec_dot_type = GGML_TYPE_Q8_0,
|
.vec_dot_type = GGML_TYPE_Q8_0,
|
||||||
},
|
},
|
||||||
[GGML_TYPE_Q5_1] = {
|
[GGML_TYPE_Q5_1] = {
|
||||||
|
.type_name = "q5_1",
|
||||||
|
.blck_size = QK5_1,
|
||||||
|
.type_size = sizeof(block_q5_1),
|
||||||
|
.is_quantized = true,
|
||||||
.to_float = (ggml_to_float_t) dequantize_row_q5_1,
|
.to_float = (ggml_to_float_t) dequantize_row_q5_1,
|
||||||
.from_float = quantize_row_q5_1,
|
.from_float = quantize_row_q5_1,
|
||||||
.from_float_reference = (ggml_from_float_t) quantize_row_q5_1_reference,
|
.from_float_reference = (ggml_from_float_t) quantize_row_q5_1_reference,
|
||||||
|
@ -1684,6 +1726,10 @@ static const ggml_type_traits_t type_traits[GGML_TYPE_COUNT] = {
|
||||||
.vec_dot_type = GGML_TYPE_Q8_1,
|
.vec_dot_type = GGML_TYPE_Q8_1,
|
||||||
},
|
},
|
||||||
[GGML_TYPE_Q8_0] = {
|
[GGML_TYPE_Q8_0] = {
|
||||||
|
.type_name = "q8_0",
|
||||||
|
.blck_size = QK8_0,
|
||||||
|
.type_size = sizeof(block_q8_0),
|
||||||
|
.is_quantized = true,
|
||||||
.to_float = dequantize_row_q8_0,
|
.to_float = dequantize_row_q8_0,
|
||||||
.from_float = quantize_row_q8_0,
|
.from_float = quantize_row_q8_0,
|
||||||
.from_float_reference = (ggml_from_float_t) quantize_row_q8_0_reference,
|
.from_float_reference = (ggml_from_float_t) quantize_row_q8_0_reference,
|
||||||
|
@ -1691,12 +1737,20 @@ static const ggml_type_traits_t type_traits[GGML_TYPE_COUNT] = {
|
||||||
.vec_dot_type = GGML_TYPE_Q8_0,
|
.vec_dot_type = GGML_TYPE_Q8_0,
|
||||||
},
|
},
|
||||||
[GGML_TYPE_Q8_1] = {
|
[GGML_TYPE_Q8_1] = {
|
||||||
|
.type_name = "q8_1",
|
||||||
|
.blck_size = QK8_1,
|
||||||
|
.type_size = sizeof(block_q8_1),
|
||||||
|
.is_quantized = true,
|
||||||
.from_float = quantize_row_q8_1,
|
.from_float = quantize_row_q8_1,
|
||||||
.from_float_reference = (ggml_from_float_t) quantize_row_q8_1_reference,
|
.from_float_reference = (ggml_from_float_t) quantize_row_q8_1_reference,
|
||||||
.vec_dot_type = GGML_TYPE_Q8_1,
|
.vec_dot_type = GGML_TYPE_Q8_1,
|
||||||
},
|
},
|
||||||
#ifdef GGML_USE_K_QUANTS
|
#ifdef GGML_USE_K_QUANTS
|
||||||
[GGML_TYPE_Q2_K] = {
|
[GGML_TYPE_Q2_K] = {
|
||||||
|
.type_name = "q2_K",
|
||||||
|
.blck_size = QK_K,
|
||||||
|
.type_size = sizeof(block_q2_K),
|
||||||
|
.is_quantized = true,
|
||||||
.to_float = (ggml_to_float_t) dequantize_row_q2_K,
|
.to_float = (ggml_to_float_t) dequantize_row_q2_K,
|
||||||
.from_float = quantize_row_q2_K,
|
.from_float = quantize_row_q2_K,
|
||||||
.from_float_reference = (ggml_from_float_t) quantize_row_q2_K_reference,
|
.from_float_reference = (ggml_from_float_t) quantize_row_q2_K_reference,
|
||||||
|
@ -1704,6 +1758,10 @@ static const ggml_type_traits_t type_traits[GGML_TYPE_COUNT] = {
|
||||||
.vec_dot_type = GGML_TYPE_Q8_K,
|
.vec_dot_type = GGML_TYPE_Q8_K,
|
||||||
},
|
},
|
||||||
[GGML_TYPE_Q3_K] = {
|
[GGML_TYPE_Q3_K] = {
|
||||||
|
.type_name = "q3_K",
|
||||||
|
.blck_size = QK_K,
|
||||||
|
.type_size = sizeof(block_q3_K),
|
||||||
|
.is_quantized = true,
|
||||||
.to_float = (ggml_to_float_t) dequantize_row_q3_K,
|
.to_float = (ggml_to_float_t) dequantize_row_q3_K,
|
||||||
.from_float = quantize_row_q3_K,
|
.from_float = quantize_row_q3_K,
|
||||||
.from_float_reference = (ggml_from_float_t) quantize_row_q3_K_reference,
|
.from_float_reference = (ggml_from_float_t) quantize_row_q3_K_reference,
|
||||||
|
@ -1711,6 +1769,10 @@ static const ggml_type_traits_t type_traits[GGML_TYPE_COUNT] = {
|
||||||
.vec_dot_type = GGML_TYPE_Q8_K,
|
.vec_dot_type = GGML_TYPE_Q8_K,
|
||||||
},
|
},
|
||||||
[GGML_TYPE_Q4_K] = {
|
[GGML_TYPE_Q4_K] = {
|
||||||
|
.type_name = "q4_K",
|
||||||
|
.blck_size = QK_K,
|
||||||
|
.type_size = sizeof(block_q4_K),
|
||||||
|
.is_quantized = true,
|
||||||
.to_float = (ggml_to_float_t) dequantize_row_q4_K,
|
.to_float = (ggml_to_float_t) dequantize_row_q4_K,
|
||||||
.from_float = quantize_row_q4_K,
|
.from_float = quantize_row_q4_K,
|
||||||
.from_float_reference = (ggml_from_float_t) quantize_row_q4_K_reference,
|
.from_float_reference = (ggml_from_float_t) quantize_row_q4_K_reference,
|
||||||
|
@ -1718,6 +1780,10 @@ static const ggml_type_traits_t type_traits[GGML_TYPE_COUNT] = {
|
||||||
.vec_dot_type = GGML_TYPE_Q8_K,
|
.vec_dot_type = GGML_TYPE_Q8_K,
|
||||||
},
|
},
|
||||||
[GGML_TYPE_Q5_K] = {
|
[GGML_TYPE_Q5_K] = {
|
||||||
|
.type_name = "q5_K",
|
||||||
|
.blck_size = QK_K,
|
||||||
|
.type_size = sizeof(block_q5_K),
|
||||||
|
.is_quantized = true,
|
||||||
.to_float = (ggml_to_float_t) dequantize_row_q5_K,
|
.to_float = (ggml_to_float_t) dequantize_row_q5_K,
|
||||||
.from_float = quantize_row_q5_K,
|
.from_float = quantize_row_q5_K,
|
||||||
.from_float_reference = (ggml_from_float_t) quantize_row_q5_K_reference,
|
.from_float_reference = (ggml_from_float_t) quantize_row_q5_K_reference,
|
||||||
|
@ -1725,6 +1791,10 @@ static const ggml_type_traits_t type_traits[GGML_TYPE_COUNT] = {
|
||||||
.vec_dot_type = GGML_TYPE_Q8_K,
|
.vec_dot_type = GGML_TYPE_Q8_K,
|
||||||
},
|
},
|
||||||
[GGML_TYPE_Q6_K] = {
|
[GGML_TYPE_Q6_K] = {
|
||||||
|
.type_name = "q6_K",
|
||||||
|
.blck_size = QK_K,
|
||||||
|
.type_size = sizeof(block_q6_K),
|
||||||
|
.is_quantized = true,
|
||||||
.to_float = (ggml_to_float_t) dequantize_row_q6_K,
|
.to_float = (ggml_to_float_t) dequantize_row_q6_K,
|
||||||
.from_float = quantize_row_q6_K,
|
.from_float = quantize_row_q6_K,
|
||||||
.from_float_reference = (ggml_from_float_t) quantize_row_q6_K_reference,
|
.from_float_reference = (ggml_from_float_t) quantize_row_q6_K_reference,
|
||||||
|
@ -1732,15 +1802,19 @@ static const ggml_type_traits_t type_traits[GGML_TYPE_COUNT] = {
|
||||||
.vec_dot_type = GGML_TYPE_Q8_K,
|
.vec_dot_type = GGML_TYPE_Q8_K,
|
||||||
},
|
},
|
||||||
[GGML_TYPE_Q8_K] = {
|
[GGML_TYPE_Q8_K] = {
|
||||||
|
.type_name = "q8_K",
|
||||||
|
.blck_size = QK_K,
|
||||||
|
.type_size = sizeof(block_q8_K),
|
||||||
|
.is_quantized = true,
|
||||||
.from_float = quantize_row_q8_K,
|
.from_float = quantize_row_q8_K,
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
};
|
};
|
||||||
|
|
||||||
// For internal test use
|
// For internal test use
|
||||||
ggml_type_traits_t ggml_internal_get_type_traits(enum ggml_type i) {
|
ggml_type_traits_t ggml_internal_get_type_traits(enum ggml_type type) {
|
||||||
GGML_ASSERT(i < GGML_TYPE_COUNT);
|
GGML_ASSERT(type < GGML_TYPE_COUNT);
|
||||||
return type_traits[i];
|
return type_traits[type];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -3649,99 +3723,6 @@ inline static void ggml_vec_argmax_f32(const int n, int * s, const float * x) {
|
||||||
*s = idx;
|
*s = idx;
|
||||||
}
|
}
|
||||||
|
|
||||||
//
|
|
||||||
// data types
|
|
||||||
//
|
|
||||||
|
|
||||||
static const int GGML_BLCK_SIZE[GGML_TYPE_COUNT] = {
|
|
||||||
[GGML_TYPE_F32] = 1,
|
|
||||||
[GGML_TYPE_F16] = 1,
|
|
||||||
[GGML_TYPE_Q4_0] = QK4_0,
|
|
||||||
[GGML_TYPE_Q4_1] = QK4_1,
|
|
||||||
[GGML_TYPE_Q5_0] = QK5_0,
|
|
||||||
[GGML_TYPE_Q5_1] = QK5_1,
|
|
||||||
[GGML_TYPE_Q8_0] = QK8_0,
|
|
||||||
[GGML_TYPE_Q8_1] = QK8_1,
|
|
||||||
#ifdef GGML_USE_K_QUANTS
|
|
||||||
[GGML_TYPE_Q2_K] = QK_K,
|
|
||||||
[GGML_TYPE_Q3_K] = QK_K,
|
|
||||||
[GGML_TYPE_Q4_K] = QK_K,
|
|
||||||
[GGML_TYPE_Q5_K] = QK_K,
|
|
||||||
[GGML_TYPE_Q6_K] = QK_K,
|
|
||||||
[GGML_TYPE_Q8_K] = QK_K,
|
|
||||||
#endif
|
|
||||||
[GGML_TYPE_I8] = 1,
|
|
||||||
[GGML_TYPE_I16] = 1,
|
|
||||||
[GGML_TYPE_I32] = 1,
|
|
||||||
};
|
|
||||||
static_assert(GGML_TYPE_COUNT == 19, "GGML_BLCK_SIZE is outdated");
|
|
||||||
|
|
||||||
static const size_t GGML_TYPE_SIZE[GGML_TYPE_COUNT] = {
|
|
||||||
[GGML_TYPE_F32] = sizeof(float),
|
|
||||||
[GGML_TYPE_F16] = sizeof(ggml_fp16_t),
|
|
||||||
[GGML_TYPE_Q4_0] = sizeof(block_q4_0),
|
|
||||||
[GGML_TYPE_Q4_1] = sizeof(block_q4_1),
|
|
||||||
[GGML_TYPE_Q5_0] = sizeof(block_q5_0),
|
|
||||||
[GGML_TYPE_Q5_1] = sizeof(block_q5_1),
|
|
||||||
[GGML_TYPE_Q8_0] = sizeof(block_q8_0),
|
|
||||||
[GGML_TYPE_Q8_1] = sizeof(block_q8_1),
|
|
||||||
#ifdef GGML_USE_K_QUANTS
|
|
||||||
[GGML_TYPE_Q2_K] = sizeof(block_q2_K),
|
|
||||||
[GGML_TYPE_Q3_K] = sizeof(block_q3_K),
|
|
||||||
[GGML_TYPE_Q4_K] = sizeof(block_q4_K),
|
|
||||||
[GGML_TYPE_Q5_K] = sizeof(block_q5_K),
|
|
||||||
[GGML_TYPE_Q6_K] = sizeof(block_q6_K),
|
|
||||||
[GGML_TYPE_Q8_K] = sizeof(block_q8_K),
|
|
||||||
#endif
|
|
||||||
[GGML_TYPE_I8] = sizeof(int8_t),
|
|
||||||
[GGML_TYPE_I16] = sizeof(int16_t),
|
|
||||||
[GGML_TYPE_I32] = sizeof(int32_t),
|
|
||||||
};
|
|
||||||
static_assert(GGML_TYPE_COUNT == 19, "GGML_TYPE_SIZE is outdated");
|
|
||||||
|
|
||||||
|
|
||||||
static const char * GGML_TYPE_NAME[GGML_TYPE_COUNT] = {
|
|
||||||
[GGML_TYPE_F32] = "f32",
|
|
||||||
[GGML_TYPE_F16] = "f16",
|
|
||||||
[GGML_TYPE_Q4_0] = "q4_0",
|
|
||||||
[GGML_TYPE_Q4_1] = "q4_1",
|
|
||||||
[GGML_TYPE_Q5_0] = "q5_0",
|
|
||||||
[GGML_TYPE_Q5_1] = "q5_1",
|
|
||||||
[GGML_TYPE_Q8_0] = "q8_0",
|
|
||||||
[GGML_TYPE_Q8_1] = "q8_1",
|
|
||||||
[GGML_TYPE_Q2_K] = "q2_K",
|
|
||||||
[GGML_TYPE_Q3_K] = "q3_K",
|
|
||||||
[GGML_TYPE_Q4_K] = "q4_K",
|
|
||||||
[GGML_TYPE_Q5_K] = "q5_K",
|
|
||||||
[GGML_TYPE_Q6_K] = "q6_K",
|
|
||||||
[GGML_TYPE_Q8_K] = "q8_K",
|
|
||||||
[GGML_TYPE_I8] = "i8",
|
|
||||||
[GGML_TYPE_I16] = "i16",
|
|
||||||
[GGML_TYPE_I32] = "i32",
|
|
||||||
};
|
|
||||||
static_assert(GGML_TYPE_COUNT == 19, "GGML_TYPE_NAME is outdated");
|
|
||||||
|
|
||||||
static bool GGML_IS_QUANTIZED[GGML_TYPE_COUNT] = {
|
|
||||||
[GGML_TYPE_F32] = false,
|
|
||||||
[GGML_TYPE_F16] = false,
|
|
||||||
[GGML_TYPE_Q4_0] = true,
|
|
||||||
[GGML_TYPE_Q4_1] = true,
|
|
||||||
[GGML_TYPE_Q5_0] = true,
|
|
||||||
[GGML_TYPE_Q5_1] = true,
|
|
||||||
[GGML_TYPE_Q8_0] = true,
|
|
||||||
[GGML_TYPE_Q8_1] = true,
|
|
||||||
[GGML_TYPE_Q2_K] = true,
|
|
||||||
[GGML_TYPE_Q3_K] = true,
|
|
||||||
[GGML_TYPE_Q4_K] = true,
|
|
||||||
[GGML_TYPE_Q5_K] = true,
|
|
||||||
[GGML_TYPE_Q6_K] = true,
|
|
||||||
[GGML_TYPE_Q8_K] = true,
|
|
||||||
[GGML_TYPE_I8] = false,
|
|
||||||
[GGML_TYPE_I16] = false,
|
|
||||||
[GGML_TYPE_I32] = false,
|
|
||||||
};
|
|
||||||
static_assert(GGML_TYPE_COUNT == 19, "GGML_IS_QUANTIZED is outdated");
|
|
||||||
|
|
||||||
static const char * GGML_OP_NAME[GGML_OP_COUNT] = {
|
static const char * GGML_OP_NAME[GGML_OP_COUNT] = {
|
||||||
"NONE",
|
"NONE",
|
||||||
|
|
||||||
|
@ -4111,29 +4092,33 @@ size_t ggml_nbytes(const struct ggml_tensor * tensor) {
|
||||||
//
|
//
|
||||||
// is enough, but just in case, adding the second part
|
// is enough, but just in case, adding the second part
|
||||||
|
|
||||||
return GGML_PAD(MAX(tensor->ne[3]*tensor->nb[3], (ggml_nelements(tensor)*GGML_TYPE_SIZE[tensor->type])/GGML_BLCK_SIZE[tensor->type]), GGML_MEM_ALIGN);
|
return GGML_PAD(MAX(tensor->ne[3]*tensor->nb[3], ggml_nelements(tensor)*ggml_type_size(tensor->type))/ggml_blck_size(tensor->type), GGML_MEM_ALIGN);
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t ggml_nbytes_split(const struct ggml_tensor * tensor, int nrows_split) {
|
size_t ggml_nbytes_split(const struct ggml_tensor * tensor, int nrows_split) {
|
||||||
static_assert(GGML_MAX_DIMS == 4, "GGML_MAX_DIMS is not 4 - update this function");
|
static_assert(GGML_MAX_DIMS == 4, "GGML_MAX_DIMS is not 4 - update this function");
|
||||||
|
|
||||||
return (nrows_split*tensor->ne[0]*GGML_TYPE_SIZE[tensor->type])/GGML_BLCK_SIZE[tensor->type];
|
return (nrows_split*tensor->ne[0]*ggml_type_size(tensor->type))/ggml_blck_size(tensor->type);
|
||||||
}
|
}
|
||||||
|
|
||||||
int ggml_blck_size(enum ggml_type type) {
|
int ggml_blck_size(enum ggml_type type) {
|
||||||
return GGML_BLCK_SIZE[type];
|
return type_traits[type].blck_size;
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t ggml_type_size(enum ggml_type type) {
|
size_t ggml_type_size(enum ggml_type type) {
|
||||||
return GGML_TYPE_SIZE[type];
|
return type_traits[type].type_size;
|
||||||
}
|
}
|
||||||
|
|
||||||
float ggml_type_sizef(enum ggml_type type) {
|
float ggml_type_sizef(enum ggml_type type) {
|
||||||
return ((float)(GGML_TYPE_SIZE[type]))/GGML_BLCK_SIZE[type];
|
return ((float)(type_traits[type].type_size))/type_traits[type].blck_size;
|
||||||
}
|
}
|
||||||
|
|
||||||
const char * ggml_type_name(enum ggml_type type) {
|
const char * ggml_type_name(enum ggml_type type) {
|
||||||
return GGML_TYPE_NAME[type];
|
return type_traits[type].type_name;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool ggml_is_quantized(enum ggml_type type) {
|
||||||
|
return type_traits[type].is_quantized;
|
||||||
}
|
}
|
||||||
|
|
||||||
const char * ggml_op_name(enum ggml_op op) {
|
const char * ggml_op_name(enum ggml_op op) {
|
||||||
|
@ -4145,7 +4130,7 @@ const char * ggml_op_symbol(enum ggml_op op) {
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t ggml_element_size(const struct ggml_tensor * tensor) {
|
size_t ggml_element_size(const struct ggml_tensor * tensor) {
|
||||||
return GGML_TYPE_SIZE[tensor->type];
|
return ggml_type_size(tensor->type);
|
||||||
}
|
}
|
||||||
|
|
||||||
static inline bool ggml_is_scalar(const struct ggml_tensor * tensor) {
|
static inline bool ggml_is_scalar(const struct ggml_tensor * tensor) {
|
||||||
|
@ -4183,10 +4168,6 @@ static inline bool ggml_can_out_prod(const struct ggml_tensor * t0, const struct
|
||||||
(t0->ne[3] == t1->ne[3]);
|
(t0->ne[3] == t1->ne[3]);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool ggml_is_quantized(enum ggml_type type) {
|
|
||||||
return GGML_IS_QUANTIZED[type];
|
|
||||||
}
|
|
||||||
|
|
||||||
enum ggml_type ggml_ftype_to_ggml_type(enum ggml_ftype ftype) {
|
enum ggml_type ggml_ftype_to_ggml_type(enum ggml_ftype ftype) {
|
||||||
enum ggml_type wtype = GGML_TYPE_COUNT;
|
enum ggml_type wtype = GGML_TYPE_COUNT;
|
||||||
|
|
||||||
|
@ -4224,8 +4205,8 @@ bool ggml_is_contiguous(const struct ggml_tensor * tensor) {
|
||||||
static_assert(GGML_MAX_DIMS == 4, "GGML_MAX_DIMS is not 4 - update this function");
|
static_assert(GGML_MAX_DIMS == 4, "GGML_MAX_DIMS is not 4 - update this function");
|
||||||
|
|
||||||
return
|
return
|
||||||
tensor->nb[0] == GGML_TYPE_SIZE[tensor->type] &&
|
tensor->nb[0] == ggml_type_size(tensor->type) &&
|
||||||
tensor->nb[1] == (tensor->nb[0]*tensor->ne[0])/GGML_BLCK_SIZE[tensor->type] &&
|
tensor->nb[1] == (tensor->nb[0]*tensor->ne[0])/ggml_blck_size(tensor->type) &&
|
||||||
tensor->nb[2] == tensor->nb[1]*tensor->ne[1] &&
|
tensor->nb[2] == tensor->nb[1]*tensor->ne[1] &&
|
||||||
tensor->nb[3] == tensor->nb[2]*tensor->ne[2];
|
tensor->nb[3] == tensor->nb[2]*tensor->ne[2];
|
||||||
}
|
}
|
||||||
|
@ -4234,7 +4215,7 @@ static inline bool ggml_is_contiguous_except_dim_1(const struct ggml_tensor * te
|
||||||
static_assert(GGML_MAX_DIMS == 4, "GGML_MAX_DIMS is not 4 - update this function");
|
static_assert(GGML_MAX_DIMS == 4, "GGML_MAX_DIMS is not 4 - update this function");
|
||||||
|
|
||||||
return
|
return
|
||||||
tensor->nb[0] == GGML_TYPE_SIZE[tensor->type] &&
|
tensor->nb[0] == ggml_type_size(tensor->type) &&
|
||||||
tensor->nb[2] == tensor->nb[1]*tensor->ne[1] &&
|
tensor->nb[2] == tensor->nb[1]*tensor->ne[1] &&
|
||||||
tensor->nb[3] == tensor->nb[2]*tensor->ne[2];
|
tensor->nb[3] == tensor->nb[2]*tensor->ne[2];
|
||||||
}
|
}
|
||||||
|
@ -4249,7 +4230,7 @@ static inline bool ggml_is_padded_1d(const struct ggml_tensor * tensor) {
|
||||||
static_assert(GGML_MAX_DIMS == 4, "GGML_MAX_DIMS is not 4 - update this function");
|
static_assert(GGML_MAX_DIMS == 4, "GGML_MAX_DIMS is not 4 - update this function");
|
||||||
|
|
||||||
return
|
return
|
||||||
tensor->nb[0] == GGML_TYPE_SIZE[tensor->type] &&
|
tensor->nb[0] == ggml_type_size(tensor->type) &&
|
||||||
tensor->nb[2] == tensor->nb[1]*tensor->ne[1] &&
|
tensor->nb[2] == tensor->nb[1]*tensor->ne[1] &&
|
||||||
tensor->nb[3] == tensor->nb[2]*tensor->ne[2];
|
tensor->nb[3] == tensor->nb[2]*tensor->ne[2];
|
||||||
}
|
}
|
||||||
|
@ -4568,7 +4549,7 @@ static struct ggml_tensor * ggml_new_tensor_impl(
|
||||||
size_t data_size = 0;
|
size_t data_size = 0;
|
||||||
|
|
||||||
if (data == NULL && !ctx->no_alloc) {
|
if (data == NULL && !ctx->no_alloc) {
|
||||||
data_size += GGML_TYPE_SIZE[type]*(ne[0]/GGML_BLCK_SIZE[type]);
|
data_size += ggml_type_size(type)*(ne[0]/ggml_blck_size(type));
|
||||||
for (int i = 1; i < n_dims; i++) {
|
for (int i = 1; i < n_dims; i++) {
|
||||||
data_size *= ne[i];
|
data_size *= ne[i];
|
||||||
}
|
}
|
||||||
|
@ -4623,8 +4604,8 @@ static struct ggml_tensor * ggml_new_tensor_impl(
|
||||||
result->ne[i] = ne[i];
|
result->ne[i] = ne[i];
|
||||||
}
|
}
|
||||||
|
|
||||||
result->nb[0] = GGML_TYPE_SIZE[type];
|
result->nb[0] = ggml_type_size(type);
|
||||||
result->nb[1] = result->nb[0]*(result->ne[0]/GGML_BLCK_SIZE[type]);
|
result->nb[1] = result->nb[0]*(result->ne[0]/ggml_blck_size(type));
|
||||||
for (int i = 2; i < GGML_MAX_DIMS; i++) {
|
for (int i = 2; i < GGML_MAX_DIMS; i++) {
|
||||||
result->nb[i] = result->nb[i - 1]*result->ne[i - 1];
|
result->nb[i] = result->nb[i - 1]*result->ne[i - 1];
|
||||||
}
|
}
|
||||||
|
@ -7746,7 +7727,7 @@ static void ggml_compute_forward_dup_same_cont(
|
||||||
memcpy(
|
memcpy(
|
||||||
((char *) dst->data + ie0*nb0),
|
((char *) dst->data + ie0*nb0),
|
||||||
((char *) src0->data + ie0*nb00),
|
((char *) src0->data + ie0*nb00),
|
||||||
(ie1 - ie0) * GGML_TYPE_SIZE[src0->type]);
|
(ie1 - ie0) * ggml_type_size(src0->type));
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -7780,7 +7761,7 @@ static void ggml_compute_forward_dup_f16(
|
||||||
|
|
||||||
if (src0->type == dst->type &&
|
if (src0->type == dst->type &&
|
||||||
ne00 == ne0 &&
|
ne00 == ne0 &&
|
||||||
nb00 == GGML_TYPE_SIZE[src0->type] && nb0 == GGML_TYPE_SIZE[dst->type]) {
|
nb00 == ggml_type_size(src0->type) && nb0 == ggml_type_size(dst->type)) {
|
||||||
// copy by rows
|
// copy by rows
|
||||||
const size_t rs = ne00*nb00;
|
const size_t rs = ne00*nb00;
|
||||||
for (int64_t i03 = 0; i03 < ne03; i03++) {
|
for (int64_t i03 = 0; i03 < ne03; i03++) {
|
||||||
|
@ -7838,7 +7819,7 @@ static void ggml_compute_forward_dup_f16(
|
||||||
float * src0_f32 = (float *) params->wdata + (ne00 + CACHE_LINE_SIZE_F32) * ith;
|
float * src0_f32 = (float *) params->wdata + (ne00 + CACHE_LINE_SIZE_F32) * ith;
|
||||||
|
|
||||||
size_t id = 0;
|
size_t id = 0;
|
||||||
size_t rs = nb0 * (ne00 / GGML_BLCK_SIZE[dst->type]);
|
size_t rs = nb0 * (ne00 / ggml_blck_size(dst->type));
|
||||||
char * dst_ptr = (char *) dst->data;
|
char * dst_ptr = (char *) dst->data;
|
||||||
|
|
||||||
for (int i03 = 0; i03 < ne03; i03++) {
|
for (int i03 = 0; i03 < ne03; i03++) {
|
||||||
|
@ -8051,7 +8032,7 @@ static void ggml_compute_forward_dup_f32(
|
||||||
|
|
||||||
if (src0->type == dst->type &&
|
if (src0->type == dst->type &&
|
||||||
ne00 == ne0 &&
|
ne00 == ne0 &&
|
||||||
nb00 == GGML_TYPE_SIZE[src0->type] && nb0 == GGML_TYPE_SIZE[dst->type]) {
|
nb00 == ggml_type_size(src0->type) && nb0 == ggml_type_size(dst->type)) {
|
||||||
// copy by rows
|
// copy by rows
|
||||||
const size_t rs = ne00*nb00;
|
const size_t rs = ne00*nb00;
|
||||||
for (int64_t i03 = 0; i03 < ne03; i03++) {
|
for (int64_t i03 = 0; i03 < ne03; i03++) {
|
||||||
|
@ -8090,7 +8071,7 @@ static void ggml_compute_forward_dup_f32(
|
||||||
ggml_from_float_t const quantize_row_q = type_traits[dst->type].from_float;
|
ggml_from_float_t const quantize_row_q = type_traits[dst->type].from_float;
|
||||||
|
|
||||||
size_t id = 0;
|
size_t id = 0;
|
||||||
size_t rs = nb0 * (ne00 / GGML_BLCK_SIZE[dst->type]);
|
size_t rs = nb0 * (ne00 / ggml_blck_size(dst->type));
|
||||||
char * dst_ptr = (char *) dst->data;
|
char * dst_ptr = (char *) dst->data;
|
||||||
|
|
||||||
for (int i03 = 0; i03 < ne03; i03++) {
|
for (int i03 = 0; i03 < ne03; i03++) {
|
||||||
|
@ -8502,7 +8483,7 @@ static void ggml_compute_forward_add_q_f32(
|
||||||
ggml_from_float_t const quantize_row_q = type_traits[type].from_float;
|
ggml_from_float_t const quantize_row_q = type_traits[type].from_float;
|
||||||
|
|
||||||
// we don't support permuted src0 or src1
|
// we don't support permuted src0 or src1
|
||||||
GGML_ASSERT(nb00 == GGML_TYPE_SIZE[type]);
|
GGML_ASSERT(nb00 == ggml_type_size(type));
|
||||||
GGML_ASSERT(nb10 == sizeof(float));
|
GGML_ASSERT(nb10 == sizeof(float));
|
||||||
|
|
||||||
// dst cannot be transposed or permuted
|
// dst cannot be transposed or permuted
|
||||||
|
@ -8776,7 +8757,7 @@ static void ggml_compute_forward_add1_q_f32(
|
||||||
ggml_from_float_t const quantize_row_q = type_traits[type].from_float;
|
ggml_from_float_t const quantize_row_q = type_traits[type].from_float;
|
||||||
|
|
||||||
// we don't support permuted src0
|
// we don't support permuted src0
|
||||||
GGML_ASSERT(nb00 == GGML_TYPE_SIZE[type]);
|
GGML_ASSERT(nb00 == ggml_type_size(type));
|
||||||
|
|
||||||
// dst cannot be transposed or permuted
|
// dst cannot be transposed or permuted
|
||||||
GGML_ASSERT(nb0 <= nb1);
|
GGML_ASSERT(nb0 <= nb1);
|
||||||
|
@ -10630,7 +10611,7 @@ static void ggml_compute_forward_mul_mat(
|
||||||
GGML_ASSERT(ne3 == ne13);
|
GGML_ASSERT(ne3 == ne13);
|
||||||
|
|
||||||
// we don't support permuted src0 or src1
|
// we don't support permuted src0 or src1
|
||||||
GGML_ASSERT(nb00 == GGML_TYPE_SIZE[type]);
|
GGML_ASSERT(nb00 == ggml_type_size(type));
|
||||||
GGML_ASSERT(nb10 == sizeof(float));
|
GGML_ASSERT(nb10 == sizeof(float));
|
||||||
|
|
||||||
// dst cannot be transposed or permuted
|
// dst cannot be transposed or permuted
|
||||||
|
@ -10708,7 +10689,7 @@ static void ggml_compute_forward_mul_mat(
|
||||||
if (params->type == GGML_TASK_INIT) {
|
if (params->type == GGML_TASK_INIT) {
|
||||||
if (src1->type != vec_dot_type) {
|
if (src1->type != vec_dot_type) {
|
||||||
char * wdata = params->wdata;
|
char * wdata = params->wdata;
|
||||||
const size_t row_size = ne10*GGML_TYPE_SIZE[vec_dot_type]/GGML_BLCK_SIZE[vec_dot_type];
|
const size_t row_size = ne10*ggml_type_size(vec_dot_type)/ggml_blck_size(vec_dot_type);
|
||||||
|
|
||||||
for (int64_t i13 = 0; i13 < ne13; ++i13) {
|
for (int64_t i13 = 0; i13 < ne13; ++i13) {
|
||||||
for (int64_t i12 = 0; i12 < ne12; ++i12) {
|
for (int64_t i12 = 0; i12 < ne12; ++i12) {
|
||||||
|
@ -10728,7 +10709,7 @@ static void ggml_compute_forward_mul_mat(
|
||||||
}
|
}
|
||||||
|
|
||||||
const void * wdata = (src1->type == vec_dot_type) ? src1->data : params->wdata;
|
const void * wdata = (src1->type == vec_dot_type) ? src1->data : params->wdata;
|
||||||
const size_t row_size = ne10*GGML_TYPE_SIZE[vec_dot_type]/GGML_BLCK_SIZE[vec_dot_type];
|
const size_t row_size = ne10*ggml_type_size(vec_dot_type)/ggml_blck_size(vec_dot_type);
|
||||||
|
|
||||||
const int64_t nr0 = ne01; // src0 rows
|
const int64_t nr0 = ne01; // src0 rows
|
||||||
const int64_t nr1 = ne11*ne12*ne13; // src1 rows
|
const int64_t nr1 = ne11*ne12*ne13; // src1 rows
|
||||||
|
@ -11201,7 +11182,7 @@ static void ggml_compute_forward_get_rows_q(
|
||||||
|
|
||||||
assert( dst->ne[0] == nc);
|
assert( dst->ne[0] == nc);
|
||||||
assert( dst->ne[1] == nr);
|
assert( dst->ne[1] == nr);
|
||||||
assert(src0->nb[0] == GGML_TYPE_SIZE[type]);
|
assert(src0->nb[0] == ggml_type_size(type));
|
||||||
|
|
||||||
for (int i = 0; i < nr; ++i) {
|
for (int i = 0; i < nr; ++i) {
|
||||||
const int r = ((int32_t *) src1->data)[i];
|
const int r = ((int32_t *) src1->data)[i];
|
||||||
|
@ -16382,7 +16363,7 @@ struct ggml_cplan ggml_graph_plan(struct ggml_cgraph * cgraph, int n_threads) {
|
||||||
|
|
||||||
size_t cur = 0;
|
size_t cur = 0;
|
||||||
if (ggml_is_quantized(node->type)) {
|
if (ggml_is_quantized(node->type)) {
|
||||||
cur = GGML_TYPE_SIZE[GGML_TYPE_F32] * node->ne[0] * n_tasks;
|
cur = ggml_type_size(GGML_TYPE_F32) * node->ne[0] * n_tasks;
|
||||||
}
|
}
|
||||||
|
|
||||||
work_size = MAX(work_size, cur);
|
work_size = MAX(work_size, cur);
|
||||||
|
@ -16395,7 +16376,7 @@ struct ggml_cplan ggml_graph_plan(struct ggml_cgraph * cgraph, int n_threads) {
|
||||||
size_t cur = 0;
|
size_t cur = 0;
|
||||||
|
|
||||||
if (ggml_is_quantized(node->src[0]->type)) {
|
if (ggml_is_quantized(node->src[0]->type)) {
|
||||||
cur = GGML_TYPE_SIZE[GGML_TYPE_F32] * node->src[0]->ne[0] * n_tasks;
|
cur = ggml_type_size(GGML_TYPE_F32) * node->src[0]->ne[0] * n_tasks;
|
||||||
}
|
}
|
||||||
|
|
||||||
work_size = MAX(work_size, cur);
|
work_size = MAX(work_size, cur);
|
||||||
|
@ -16407,7 +16388,7 @@ struct ggml_cplan ggml_graph_plan(struct ggml_cgraph * cgraph, int n_threads) {
|
||||||
size_t cur = 0;
|
size_t cur = 0;
|
||||||
|
|
||||||
if (ggml_is_quantized(node->src[0]->type)) {
|
if (ggml_is_quantized(node->src[0]->type)) {
|
||||||
cur = GGML_TYPE_SIZE[GGML_TYPE_F32] * node->src[1]->ne[0] * n_tasks;
|
cur = ggml_type_size(GGML_TYPE_F32) * node->src[1]->ne[0] * n_tasks;
|
||||||
}
|
}
|
||||||
|
|
||||||
work_size = MAX(work_size, cur);
|
work_size = MAX(work_size, cur);
|
||||||
|
@ -16490,12 +16471,12 @@ struct ggml_cplan ggml_graph_plan(struct ggml_cgraph * cgraph, int n_threads) {
|
||||||
// the threads are still spinning
|
// the threads are still spinning
|
||||||
if (node->src[0]->type != GGML_TYPE_F32) {
|
if (node->src[0]->type != GGML_TYPE_F32) {
|
||||||
// here we need memory just for single 2D matrix from src0
|
// here we need memory just for single 2D matrix from src0
|
||||||
cur = GGML_TYPE_SIZE[GGML_TYPE_F32]*(node->src[0]->ne[0]*node->src[0]->ne[1]);
|
cur = ggml_type_size(GGML_TYPE_F32)*(node->src[0]->ne[0]*node->src[0]->ne[1]);
|
||||||
}
|
}
|
||||||
} else
|
} else
|
||||||
#endif
|
#endif
|
||||||
if (node->src[1]->type != vec_dot_type) {
|
if (node->src[1]->type != vec_dot_type) {
|
||||||
cur = GGML_TYPE_SIZE[vec_dot_type]*ggml_nelements(node->src[1])/GGML_BLCK_SIZE[vec_dot_type];
|
cur = ggml_type_size(vec_dot_type)*ggml_nelements(node->src[1])/ggml_blck_size(vec_dot_type);
|
||||||
} else {
|
} else {
|
||||||
cur = 0;
|
cur = 0;
|
||||||
}
|
}
|
||||||
|
@ -18301,8 +18282,8 @@ enum ggml_opt_result ggml_opt_resume(
|
||||||
struct ggml_tensor * f) {
|
struct ggml_tensor * f) {
|
||||||
|
|
||||||
// build forward + backward compute graphs
|
// build forward + backward compute graphs
|
||||||
struct ggml_tensor * gfbuf = ggml_new_tensor_1d(ctx, GGML_TYPE_I32, sizeof(struct ggml_cgraph) / GGML_TYPE_SIZE[GGML_TYPE_I32]+ (sizeof(struct ggml_cgraph) % GGML_TYPE_SIZE[GGML_TYPE_I32] ? 1 : 0));
|
struct ggml_tensor * gfbuf = ggml_new_tensor_1d(ctx, GGML_TYPE_I32, sizeof(struct ggml_cgraph) / ggml_type_size(GGML_TYPE_I32)+ (sizeof(struct ggml_cgraph) % ggml_type_size(GGML_TYPE_I32) ? 1 : 0));
|
||||||
struct ggml_tensor * gbbuf = ggml_new_tensor_1d(ctx, GGML_TYPE_I32, sizeof(struct ggml_cgraph) / GGML_TYPE_SIZE[GGML_TYPE_I32]+ (sizeof(struct ggml_cgraph) % GGML_TYPE_SIZE[GGML_TYPE_I32] ? 1 : 0));
|
struct ggml_tensor * gbbuf = ggml_new_tensor_1d(ctx, GGML_TYPE_I32, sizeof(struct ggml_cgraph) / ggml_type_size(GGML_TYPE_I32)+ (sizeof(struct ggml_cgraph) % ggml_type_size(GGML_TYPE_I32) ? 1 : 0));
|
||||||
|
|
||||||
struct ggml_cgraph * gf = (struct ggml_cgraph *) gfbuf->data;
|
struct ggml_cgraph * gf = (struct ggml_cgraph *) gfbuf->data;
|
||||||
struct ggml_cgraph * gb = (struct ggml_cgraph *) gbbuf->data;
|
struct ggml_cgraph * gb = (struct ggml_cgraph *) gbbuf->data;
|
||||||
|
|
6
ggml.h
6
ggml.h
|
@ -1740,6 +1740,10 @@ extern "C" {
|
||||||
typedef void (*ggml_vec_dot_t) (const int n, float * GGML_RESTRICT s, const void * GGML_RESTRICT x, const void * GGML_RESTRICT y);
|
typedef void (*ggml_vec_dot_t) (const int n, float * GGML_RESTRICT s, const void * GGML_RESTRICT x, const void * GGML_RESTRICT y);
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
|
const char * type_name;
|
||||||
|
int blck_size;
|
||||||
|
size_t type_size;
|
||||||
|
bool is_quantized;
|
||||||
ggml_to_float_t to_float;
|
ggml_to_float_t to_float;
|
||||||
ggml_from_float_t from_float;
|
ggml_from_float_t from_float;
|
||||||
ggml_from_float_t from_float_reference;
|
ggml_from_float_t from_float_reference;
|
||||||
|
@ -1747,7 +1751,7 @@ extern "C" {
|
||||||
enum ggml_type vec_dot_type;
|
enum ggml_type vec_dot_type;
|
||||||
} ggml_type_traits_t;
|
} ggml_type_traits_t;
|
||||||
|
|
||||||
ggml_type_traits_t ggml_internal_get_type_traits(enum ggml_type i);
|
ggml_type_traits_t ggml_internal_get_type_traits(enum ggml_type type);
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
|
|
252
llama.cpp
252
llama.cpp
|
@ -64,7 +64,7 @@ static void llama_log_callback_default(llama_log_level level, const char * text,
|
||||||
#define LLAMA_LOG_ERROR(...) llama_log_internal(LLAMA_LOG_LEVEL_ERROR, __VA_ARGS__)
|
#define LLAMA_LOG_ERROR(...) llama_log_internal(LLAMA_LOG_LEVEL_ERROR, __VA_ARGS__)
|
||||||
|
|
||||||
|
|
||||||
#if !defined(GGML_USE_CUBLAS) && !defined(GGML_USE_METAL)
|
#if !defined(GGML_USE_CUBLAS)
|
||||||
#include "ggml-alloc.h"
|
#include "ggml-alloc.h"
|
||||||
#define LLAMA_USE_ALLOCATOR
|
#define LLAMA_USE_ALLOCATOR
|
||||||
#else
|
#else
|
||||||
|
@ -116,15 +116,15 @@ static void ggml_graph_compute_helper(std::vector<uint8_t> & buf, ggml_cgraph *
|
||||||
// memory sizes (calculated for n_batch == 512)
|
// memory sizes (calculated for n_batch == 512)
|
||||||
//
|
//
|
||||||
|
|
||||||
static const std::map<e_model, size_t> & MEM_REQ_SCRATCH0(int n_ctx)
|
static std::map<e_model, size_t> MEM_REQ_SCRATCH0(int n_ctx)
|
||||||
{
|
{
|
||||||
static std::map<e_model, size_t> k_sizes = {
|
std::map<e_model, size_t> k_sizes = {
|
||||||
{ MODEL_3B, ((size_t) n_ctx / 16ull + 156ull) * MB },
|
{ MODEL_3B, ((size_t) n_ctx / 16ull + 156ull) * MB },
|
||||||
{ MODEL_7B, ((size_t) n_ctx / 16ull + 164ull) * MB },
|
{ MODEL_7B, ((size_t) n_ctx / 16ull + 164ull) * MB },
|
||||||
{ MODEL_13B, ((size_t) n_ctx / 12ull + 184ull) * MB },
|
{ MODEL_13B, ((size_t) n_ctx / 12ull + 184ull) * MB },
|
||||||
{ MODEL_30B, ((size_t) n_ctx / 9ull + 224ull) * MB },
|
{ MODEL_30B, ((size_t) n_ctx / 9ull + 224ull) * MB },
|
||||||
{ MODEL_65B, ((size_t) n_ctx / 6ull + 320ull) * MB }, // guess
|
{ MODEL_65B, ((size_t) n_ctx / 6ull + 320ull) * MB }, // guess
|
||||||
{ MODEL_70B, ((size_t) n_ctx / 6ull + 320ull) * MB },
|
{ MODEL_70B, ((size_t) n_ctx / 7ull + 320ull) * MB },
|
||||||
};
|
};
|
||||||
return k_sizes;
|
return k_sizes;
|
||||||
}
|
}
|
||||||
|
@ -990,7 +990,7 @@ int64_t llama_time_us() {
|
||||||
// model loading
|
// model loading
|
||||||
//
|
//
|
||||||
|
|
||||||
static const char *llama_file_version_name(llama_file_version version) {
|
static const char * llama_file_version_name(llama_file_version version) {
|
||||||
switch (version) {
|
switch (version) {
|
||||||
case LLAMA_FILE_VERSION_GGML: return "'ggml' (old version with low tokenizer quality and no mmap support)";
|
case LLAMA_FILE_VERSION_GGML: return "'ggml' (old version with low tokenizer quality and no mmap support)";
|
||||||
case LLAMA_FILE_VERSION_GGMF_V1: return "ggmf v1 (old version with no mmap support)";
|
case LLAMA_FILE_VERSION_GGMF_V1: return "ggmf v1 (old version with no mmap support)";
|
||||||
|
@ -1002,7 +1002,7 @@ static const char *llama_file_version_name(llama_file_version version) {
|
||||||
return "unknown";
|
return "unknown";
|
||||||
}
|
}
|
||||||
|
|
||||||
static const char *llama_ftype_name(enum llama_ftype ftype) {
|
const char * llama_ftype_name(enum llama_ftype ftype) {
|
||||||
switch (ftype) {
|
switch (ftype) {
|
||||||
case LLAMA_FTYPE_ALL_F32: return "all F32";
|
case LLAMA_FTYPE_ALL_F32: return "all F32";
|
||||||
case LLAMA_FTYPE_MOSTLY_F16: return "mostly F16";
|
case LLAMA_FTYPE_MOSTLY_F16: return "mostly F16";
|
||||||
|
@ -1027,7 +1027,7 @@ static const char *llama_ftype_name(enum llama_ftype ftype) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static const char *llama_model_type_name(e_model type) {
|
static const char * llama_model_type_name(e_model type) {
|
||||||
switch (type) {
|
switch (type) {
|
||||||
case MODEL_3B: return "3B";
|
case MODEL_3B: return "3B";
|
||||||
case MODEL_7B: return "7B";
|
case MODEL_7B: return "7B";
|
||||||
|
@ -1621,11 +1621,11 @@ static struct ggml_cgraph * llama_build_graph(
|
||||||
ggml_set_name(Q, "Q");
|
ggml_set_name(Q, "Q");
|
||||||
|
|
||||||
struct ggml_tensor * K =
|
struct ggml_tensor * K =
|
||||||
ggml_permute(ctx0,
|
ggml_view_3d(ctx0, kv_self.k,
|
||||||
ggml_reshape_3d(ctx0,
|
n_embd_head, n_past + N, n_head_kv,
|
||||||
ggml_view_1d(ctx0, kv_self.k, (n_past + N)*n_embd_gqa, il*n_ctx*ggml_element_size(kv_self.k)*n_embd_gqa),
|
ggml_element_size(kv_self.k)*n_embd_gqa,
|
||||||
n_embd_head, n_head_kv, n_past + N),
|
ggml_element_size(kv_self.k)*n_embd_head,
|
||||||
0, 2, 1, 3);
|
ggml_element_size(kv_self.k)*n_embd_gqa*n_ctx*il);
|
||||||
offload_func_kq(K);
|
offload_func_kq(K);
|
||||||
ggml_set_name(K, "K");
|
ggml_set_name(K, "K");
|
||||||
|
|
||||||
|
@ -1654,9 +1654,9 @@ static struct ggml_cgraph * llama_build_graph(
|
||||||
struct ggml_tensor * V =
|
struct ggml_tensor * V =
|
||||||
ggml_view_3d(ctx0, kv_self.v,
|
ggml_view_3d(ctx0, kv_self.v,
|
||||||
n_past + N, n_embd_head, n_head_kv,
|
n_past + N, n_embd_head, n_head_kv,
|
||||||
n_ctx*ggml_element_size(kv_self.v),
|
ggml_element_size(kv_self.v)*n_ctx,
|
||||||
n_ctx*ggml_element_size(kv_self.v)*n_embd_head,
|
ggml_element_size(kv_self.v)*n_ctx*n_embd_head,
|
||||||
n_ctx*ggml_element_size(kv_self.v)*n_embd_gqa*il);
|
ggml_element_size(kv_self.v)*n_ctx*n_embd_gqa*il);
|
||||||
offload_func_v(V);
|
offload_func_v(V);
|
||||||
ggml_set_name(V, "V");
|
ggml_set_name(V, "V");
|
||||||
|
|
||||||
|
@ -1811,6 +1811,13 @@ static bool llama_eval_internal(
|
||||||
|
|
||||||
LLAMA_ASSERT((!tokens && embd) || (tokens && !embd));
|
LLAMA_ASSERT((!tokens && embd) || (tokens && !embd));
|
||||||
|
|
||||||
|
LLAMA_ASSERT(n_tokens > 0);
|
||||||
|
LLAMA_ASSERT(n_past >= 0);
|
||||||
|
LLAMA_ASSERT(n_threads > 0);
|
||||||
|
// TODO: keep the values of n_batch and n_ctx
|
||||||
|
// LLAMA_ASSERT(n_tokens <= n_batch);
|
||||||
|
// LLAMA_ASSERT(n_past + n_tokens <= n_ctx);
|
||||||
|
|
||||||
const int64_t t_start_us = ggml_time_us();
|
const int64_t t_start_us = ggml_time_us();
|
||||||
|
|
||||||
#ifdef GGML_USE_MPI
|
#ifdef GGML_USE_MPI
|
||||||
|
@ -1857,11 +1864,7 @@ static bool llama_eval_internal(
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifdef GGML_USE_METAL
|
#ifdef GGML_USE_METAL
|
||||||
if (lctx.ctx_metal && N == 1) {
|
if (lctx.ctx_metal) {
|
||||||
// TODO: disabled until #2413 is resolved
|
|
||||||
//if (!ggml_metal_if_optimized(lctx.ctx_metal)) {
|
|
||||||
// ggml_metal_graph_find_concurrency(lctx.ctx_metal, gf);
|
|
||||||
//}
|
|
||||||
ggml_metal_set_n_cb (lctx.ctx_metal, n_threads);
|
ggml_metal_set_n_cb (lctx.ctx_metal, n_threads);
|
||||||
ggml_metal_graph_compute(lctx.ctx_metal, gf);
|
ggml_metal_graph_compute(lctx.ctx_metal, gf);
|
||||||
ggml_metal_get_tensor (lctx.ctx_metal, res);
|
ggml_metal_get_tensor (lctx.ctx_metal, res);
|
||||||
|
@ -1869,22 +1872,6 @@ static bool llama_eval_internal(
|
||||||
ggml_metal_get_tensor(lctx.ctx_metal, embeddings);
|
ggml_metal_get_tensor(lctx.ctx_metal, embeddings);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// IMPORTANT:
|
|
||||||
// Since we don't have efficient Matrix x Matrix Metal multiplication yet, we fallback to vanilla
|
|
||||||
// ggml_graph_compute(). It uses Apple's Accelerate CBLAS API which takes advantage of the ANE or the AMX
|
|
||||||
// coprocessor.
|
|
||||||
//
|
|
||||||
// When we implement Matrix x Matrix Metal multiplication, we can avoid this branch.
|
|
||||||
// But for now, we have focused only on Matrix x Vector Metal multiplication.
|
|
||||||
//
|
|
||||||
// TODO: avoid these syncs via shared memory (ref #1696)
|
|
||||||
//
|
|
||||||
if (lctx.ctx_metal) {
|
|
||||||
// We need to sync the GPU KV cache with the CPU KV cache
|
|
||||||
ggml_metal_get_tensor(lctx.ctx_metal, kv_self.k);
|
|
||||||
ggml_metal_get_tensor(lctx.ctx_metal, kv_self.v);
|
|
||||||
}
|
|
||||||
|
|
||||||
ggml_graph_compute_helper(lctx.work_buffer, gf, n_threads);
|
ggml_graph_compute_helper(lctx.work_buffer, gf, n_threads);
|
||||||
}
|
}
|
||||||
#else
|
#else
|
||||||
|
@ -2109,37 +2096,81 @@ static std::vector<llama_vocab::id> llama_tokenize(const llama_vocab & vocab, co
|
||||||
// grammar - internal
|
// grammar - internal
|
||||||
//
|
//
|
||||||
|
|
||||||
|
struct llama_partial_utf8 {
|
||||||
|
uint32_t value; // bit value so far (unshifted)
|
||||||
|
int n_remain; // num bytes remaining; -1 indicates invalid sequence
|
||||||
|
};
|
||||||
|
|
||||||
struct llama_grammar {
|
struct llama_grammar {
|
||||||
const std::vector<std::vector<llama_grammar_element>> rules;
|
const std::vector<std::vector<llama_grammar_element>> rules;
|
||||||
std::vector<std::vector<const llama_grammar_element *>> stacks;
|
std::vector<std::vector<const llama_grammar_element *>> stacks;
|
||||||
|
|
||||||
|
// buffer for partially generated UTF-8 sequence from accepted tokens
|
||||||
|
llama_partial_utf8 partial_utf8;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct llama_grammar_candidate {
|
struct llama_grammar_candidate {
|
||||||
size_t index;
|
size_t index;
|
||||||
const uint32_t * code_points;
|
const uint32_t * code_points;
|
||||||
|
llama_partial_utf8 partial_utf8;
|
||||||
};
|
};
|
||||||
|
|
||||||
// NOTE: assumes valid utf8 (but checks for overrun)
|
// Decodes a UTF-8 string which may end in an incomplete sequence. Adds a terminating 0 for use as
|
||||||
// adds a terminating 0 for use as pointer
|
// pointer. If an invalid sequence is encountered, returns `llama_partial_utf8.n_remain == -1`.
|
||||||
std::vector<uint32_t> decode_utf8(const char * src) {
|
std::pair<std::vector<uint32_t>, llama_partial_utf8> decode_utf8(
|
||||||
static const int lookup[] = { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 3, 4 };
|
const char * src,
|
||||||
|
llama_partial_utf8 partial_start) {
|
||||||
|
static const int lookup[] = { 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 2, 2, 3, 4 };
|
||||||
const char * pos = src;
|
const char * pos = src;
|
||||||
std::vector<uint32_t> code_points;
|
std::vector<uint32_t> code_points;
|
||||||
|
uint32_t value = partial_start.value;
|
||||||
|
int n_remain = partial_start.n_remain;
|
||||||
|
|
||||||
|
// continue previous decode, if applicable
|
||||||
|
while (*pos != 0 && n_remain > 0) {
|
||||||
|
uint8_t next_byte = static_cast<uint8_t>(*pos);
|
||||||
|
if ((next_byte >> 6) != 2) {
|
||||||
|
// invalid sequence, abort
|
||||||
|
code_points.push_back(0);
|
||||||
|
return std::make_pair(std::move(code_points), llama_partial_utf8{ 0, -1 });
|
||||||
|
}
|
||||||
|
value = (value << 6) + (next_byte & 0x3F);
|
||||||
|
++pos;
|
||||||
|
--n_remain;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (partial_start.n_remain > 0 && n_remain == 0) {
|
||||||
|
code_points.push_back(value);
|
||||||
|
}
|
||||||
|
|
||||||
|
// decode any subsequent utf-8 sequences, which may end in an incomplete one
|
||||||
while (*pos != 0) {
|
while (*pos != 0) {
|
||||||
uint8_t first_byte = static_cast<uint8_t>(*pos);
|
uint8_t first_byte = static_cast<uint8_t>(*pos);
|
||||||
uint8_t highbits = first_byte >> 4;
|
uint8_t highbits = first_byte >> 4;
|
||||||
int len = lookup[highbits];
|
n_remain = lookup[highbits] - 1;
|
||||||
uint8_t mask = (1 << (8 - len)) - 1;
|
|
||||||
uint32_t value = first_byte & mask;
|
if (n_remain < 0) {
|
||||||
const char * end = pos + len; // may overrun!
|
// invalid sequence, abort
|
||||||
++pos;
|
code_points.clear();
|
||||||
for ( ; pos < end && *pos != 0; ++pos) {
|
code_points.push_back(0);
|
||||||
value = (value << 6) + (static_cast<uint8_t>(*pos) & 0x3F);
|
return std::make_pair(std::move(code_points), llama_partial_utf8{ 0, n_remain });
|
||||||
|
}
|
||||||
|
|
||||||
|
uint8_t mask = (1 << (7 - n_remain)) - 1;
|
||||||
|
value = first_byte & mask;
|
||||||
|
++pos;
|
||||||
|
while (*pos != 0 && n_remain > 0) {
|
||||||
|
value = (value << 6) + (static_cast<uint8_t>(*pos) & 0x3F);
|
||||||
|
++pos;
|
||||||
|
--n_remain;
|
||||||
|
}
|
||||||
|
if (n_remain == 0) {
|
||||||
|
code_points.push_back(value);
|
||||||
}
|
}
|
||||||
code_points.push_back(value);
|
|
||||||
}
|
}
|
||||||
code_points.push_back(0);
|
code_points.push_back(0);
|
||||||
return code_points;
|
|
||||||
|
return std::make_pair(std::move(code_points), llama_partial_utf8{ value, n_remain });
|
||||||
}
|
}
|
||||||
|
|
||||||
// returns true iff pos points to the end of one of the definitions of a rule
|
// returns true iff pos points to the end of one of the definitions of a rule
|
||||||
|
@ -2176,6 +2207,56 @@ static std::pair<bool, const llama_grammar_element *> llama_grammar_match_char(
|
||||||
return std::make_pair(found == is_positive_char, pos);
|
return std::make_pair(found == is_positive_char, pos);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// returns true iff some continuation of the given partial UTF-8 sequence could satisfy the char
|
||||||
|
// range at pos (regular or inverse range)
|
||||||
|
// asserts that pos is pointing to a char range element
|
||||||
|
static bool llama_grammar_match_partial_char(
|
||||||
|
const llama_grammar_element * pos,
|
||||||
|
const llama_partial_utf8 partial_utf8) {
|
||||||
|
|
||||||
|
bool is_positive_char = pos->type == LLAMA_GRETYPE_CHAR;
|
||||||
|
LLAMA_ASSERT(is_positive_char || pos->type == LLAMA_GRETYPE_CHAR_NOT);
|
||||||
|
|
||||||
|
uint32_t partial_value = partial_utf8.value;
|
||||||
|
int n_remain = partial_utf8.n_remain;
|
||||||
|
|
||||||
|
// invalid sequence or 7-bit char split across 2 bytes (overlong)
|
||||||
|
if (n_remain < 0 || (n_remain == 1 && partial_value < 2)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// range of possible code points this partial UTF-8 sequence could complete to
|
||||||
|
uint32_t low = partial_value << (n_remain * 6);
|
||||||
|
uint32_t high = low | ((1 << (n_remain * 6)) - 1);
|
||||||
|
|
||||||
|
if (low == 0) {
|
||||||
|
if (n_remain == 2) {
|
||||||
|
low = 1 << 11;
|
||||||
|
} else if (n_remain == 3) {
|
||||||
|
low = 1 << 16;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
do {
|
||||||
|
if (pos[1].type == LLAMA_GRETYPE_CHAR_RNG_UPPER) {
|
||||||
|
// inclusive range, e.g. [a-z]
|
||||||
|
if (pos->value <= high && low <= pos[1].value) {
|
||||||
|
return is_positive_char;
|
||||||
|
}
|
||||||
|
pos += 2;
|
||||||
|
} else {
|
||||||
|
// exact char match, e.g. [a] or "a"
|
||||||
|
if (low <= pos->value && pos->value <= high) {
|
||||||
|
return is_positive_char;
|
||||||
|
}
|
||||||
|
pos += 1;
|
||||||
|
}
|
||||||
|
} while (pos->type == LLAMA_GRETYPE_CHAR_ALT);
|
||||||
|
|
||||||
|
return !is_positive_char;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
// transforms a grammar pushdown stack into N possible stacks, all ending
|
// transforms a grammar pushdown stack into N possible stacks, all ending
|
||||||
// at a character range (terminal element)
|
// at a character range (terminal element)
|
||||||
static void llama_grammar_advance_stack(
|
static void llama_grammar_advance_stack(
|
||||||
|
@ -2276,8 +2357,11 @@ static std::vector<llama_grammar_candidate> llama_grammar_reject_candidates_for_
|
||||||
std::vector<llama_grammar_candidate> rejects;
|
std::vector<llama_grammar_candidate> rejects;
|
||||||
|
|
||||||
if (stack.empty()) {
|
if (stack.empty()) {
|
||||||
// accept nothing; EOS is handled elsewhere
|
for (auto tok : candidates) {
|
||||||
rejects.insert(rejects.end(), candidates.begin(), candidates.end());
|
if (*tok.code_points != 0 || tok.partial_utf8.n_remain != 0) {
|
||||||
|
rejects.push_back(tok);
|
||||||
|
}
|
||||||
|
}
|
||||||
return rejects;
|
return rejects;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -2285,10 +2369,15 @@ static std::vector<llama_grammar_candidate> llama_grammar_reject_candidates_for_
|
||||||
|
|
||||||
std::vector<llama_grammar_candidate> next_candidates;
|
std::vector<llama_grammar_candidate> next_candidates;
|
||||||
for (auto tok : candidates) {
|
for (auto tok : candidates) {
|
||||||
if (llama_grammar_match_char(stack_pos, tok.code_points[0]).first) {
|
if (*tok.code_points == 0) {
|
||||||
if (tok.code_points[1] != 0) {
|
// reached end of full codepoints in token, reject iff it ended in a partial sequence
|
||||||
next_candidates.push_back({ tok.index, tok.code_points + 1 });
|
// that cannot satisfy this position in grammar
|
||||||
|
if (tok.partial_utf8.n_remain != 0 &&
|
||||||
|
!llama_grammar_match_partial_char(stack_pos, tok.partial_utf8)) {
|
||||||
|
rejects.push_back(tok);
|
||||||
}
|
}
|
||||||
|
} else if (llama_grammar_match_char(stack_pos, *tok.code_points).first) {
|
||||||
|
next_candidates.push_back({ tok.index, tok.code_points + 1, tok.partial_utf8 });
|
||||||
} else {
|
} else {
|
||||||
rejects.push_back(tok);
|
rejects.push_back(tok);
|
||||||
}
|
}
|
||||||
|
@ -2306,7 +2395,7 @@ static std::vector<llama_grammar_candidate> llama_grammar_reject_candidates_for_
|
||||||
|
|
||||||
auto next_rejects = llama_grammar_reject_candidates(rules, next_stacks, next_candidates);
|
auto next_rejects = llama_grammar_reject_candidates(rules, next_stacks, next_candidates);
|
||||||
for (auto tok : next_rejects) {
|
for (auto tok : next_rejects) {
|
||||||
rejects.push_back({ tok.index, tok.code_points - 1 });
|
rejects.push_back({ tok.index, tok.code_points - 1, tok.partial_utf8 });
|
||||||
}
|
}
|
||||||
|
|
||||||
return rejects;
|
return rejects;
|
||||||
|
@ -2371,7 +2460,7 @@ struct llama_grammar * llama_grammar_init(
|
||||||
}
|
}
|
||||||
} while (true);
|
} while (true);
|
||||||
|
|
||||||
return new llama_grammar{ std::move(vec_rules), std::move(stacks) };
|
return new llama_grammar{ std::move(vec_rules), std::move(stacks), {} };
|
||||||
}
|
}
|
||||||
|
|
||||||
void llama_grammar_free(struct llama_grammar * grammar) {
|
void llama_grammar_free(struct llama_grammar * grammar) {
|
||||||
|
@ -2677,8 +2766,8 @@ void llama_sample_grammar(struct llama_context * ctx, llama_token_data_array * c
|
||||||
|
|
||||||
const llama_token eos = llama_token_eos();
|
const llama_token eos = llama_token_eos();
|
||||||
|
|
||||||
std::vector<std::vector<uint32_t>> candidates_decoded;
|
std::vector<std::pair<std::vector<uint32_t>, llama_partial_utf8>> candidates_decoded;
|
||||||
std::vector<llama_grammar_candidate> candidates_grammar;
|
std::vector<llama_grammar_candidate> candidates_grammar;
|
||||||
|
|
||||||
for (size_t i = 0; i < candidates->size; ++i) {
|
for (size_t i = 0; i < candidates->size; ++i) {
|
||||||
const llama_token id = candidates->data[i].id;
|
const llama_token id = candidates->data[i].id;
|
||||||
|
@ -2690,8 +2779,10 @@ void llama_sample_grammar(struct llama_context * ctx, llama_token_data_array * c
|
||||||
} else if (*str == 0) {
|
} else if (*str == 0) {
|
||||||
candidates->data[i].logit = -INFINITY;
|
candidates->data[i].logit = -INFINITY;
|
||||||
} else {
|
} else {
|
||||||
candidates_decoded.push_back(decode_utf8(str));
|
candidates_decoded.push_back(decode_utf8(str, grammar->partial_utf8));
|
||||||
candidates_grammar.push_back({ i, candidates_decoded.back().data() });
|
candidates_grammar.push_back({
|
||||||
|
i, candidates_decoded.back().first.data(), candidates_decoded.back().second
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -2892,11 +2983,14 @@ void llama_grammar_accept_token(struct llama_context * ctx, struct llama_grammar
|
||||||
}
|
}
|
||||||
|
|
||||||
const char * str = llama_token_to_str(ctx, token);
|
const char * str = llama_token_to_str(ctx, token);
|
||||||
|
|
||||||
// Note terminating 0 in decoded string
|
// Note terminating 0 in decoded string
|
||||||
auto code_points = decode_utf8(str);
|
const auto decoded = decode_utf8(str, grammar->partial_utf8);
|
||||||
|
const auto & code_points = decoded.first;
|
||||||
for (auto it = code_points.begin(), end = code_points.end() - 1; it != end; ++it) {
|
for (auto it = code_points.begin(), end = code_points.end() - 1; it != end; ++it) {
|
||||||
grammar->stacks = llama_grammar_accept(grammar->rules, grammar->stacks, *it);
|
grammar->stacks = llama_grammar_accept(grammar->rules, grammar->stacks, *it);
|
||||||
}
|
}
|
||||||
|
grammar->partial_utf8 = decoded.second;
|
||||||
LLAMA_ASSERT(!grammar->stacks.empty());
|
LLAMA_ASSERT(!grammar->stacks.empty());
|
||||||
|
|
||||||
ctx->t_sample_us += ggml_time_us() - t_start_sample_us;
|
ctx->t_sample_us += ggml_time_us() - t_start_sample_us;
|
||||||
|
@ -3317,7 +3411,18 @@ struct llama_context * llama_new_context_with_model(
|
||||||
int n_past = hparams.n_ctx - n_tokens;
|
int n_past = hparams.n_ctx - n_tokens;
|
||||||
llama_token token = llama_token_bos(); // not actually used by llama_build_graph, but required to choose between token and embedding inputs graph
|
llama_token token = llama_token_bos(); // not actually used by llama_build_graph, but required to choose between token and embedding inputs graph
|
||||||
ggml_cgraph * gf = llama_build_graph(*ctx, &token, NULL, n_tokens, n_past);
|
ggml_cgraph * gf = llama_build_graph(*ctx, &token, NULL, n_tokens, n_past);
|
||||||
|
#ifdef GGML_USE_METAL
|
||||||
|
if (params.n_gpu_layers > 0) {
|
||||||
|
ctx->ctx_metal = ggml_metal_init(1);
|
||||||
|
if (!ctx->ctx_metal) {
|
||||||
|
LLAMA_LOG_ERROR("%s: ggml_metal_init() failed\n", __func__);
|
||||||
|
llama_free(ctx);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
ggml_metal_graph_find_concurrency(ctx->ctx_metal, gf, false);
|
||||||
|
ggml_allocr_set_parse_seq(ctx->alloc, ggml_metal_get_concur_list(ctx->ctx_metal), ggml_metal_if_optimized(ctx->ctx_metal));
|
||||||
|
}
|
||||||
|
#endif
|
||||||
// measure memory requirements for the graph
|
// measure memory requirements for the graph
|
||||||
size_t alloc_size = ggml_allocr_alloc_graph(ctx->alloc, gf) + tensor_alignment;
|
size_t alloc_size = ggml_allocr_alloc_graph(ctx->alloc, gf) + tensor_alignment;
|
||||||
|
|
||||||
|
@ -3335,6 +3440,11 @@ struct llama_context * llama_new_context_with_model(
|
||||||
|
|
||||||
ctx->buf_alloc.resize(alloc_size);
|
ctx->buf_alloc.resize(alloc_size);
|
||||||
ctx->alloc = ggml_allocr_new(ctx->buf_alloc.addr, ctx->buf_alloc.size, tensor_alignment);
|
ctx->alloc = ggml_allocr_new(ctx->buf_alloc.addr, ctx->buf_alloc.size, tensor_alignment);
|
||||||
|
#ifdef GGML_USE_METAL
|
||||||
|
if (ctx->ctx_metal) {
|
||||||
|
ggml_allocr_set_parse_seq(ctx->alloc, ggml_metal_get_concur_list(ctx->ctx_metal), ggml_metal_if_optimized(ctx->ctx_metal));
|
||||||
|
}
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
#else
|
#else
|
||||||
ctx->buf_compute.resize(blasbatchmul*MEM_REQ_EVAL().at(ctx->model.type) + ggml_graph_overhead());
|
ctx->buf_compute.resize(blasbatchmul*MEM_REQ_EVAL().at(ctx->model.type) + ggml_graph_overhead());
|
||||||
|
@ -3349,13 +3459,6 @@ struct llama_context * llama_new_context_with_model(
|
||||||
#ifdef GGML_USE_METAL
|
#ifdef GGML_USE_METAL
|
||||||
if (params.n_gpu_layers > 0) {
|
if (params.n_gpu_layers > 0) {
|
||||||
// this allocates all Metal resources and memory buffers
|
// this allocates all Metal resources and memory buffers
|
||||||
ctx->ctx_metal = ggml_metal_init(1);
|
|
||||||
|
|
||||||
if (!ctx->ctx_metal) {
|
|
||||||
LLAMA_LOG_ERROR("%s: ggml_metal_init() failed\n", __func__);
|
|
||||||
llama_free(ctx);
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
void * data_ptr = NULL;
|
void * data_ptr = NULL;
|
||||||
size_t data_size = 0;
|
size_t data_size = 0;
|
||||||
|
@ -3384,8 +3487,7 @@ struct llama_context * llama_new_context_with_model(
|
||||||
LLAMA_METAL_CHECK_BUF(ggml_metal_add_buffer(ctx->ctx_metal, "eval", ctx->buf_compute.addr, ctx->buf_compute.size, 0));
|
LLAMA_METAL_CHECK_BUF(ggml_metal_add_buffer(ctx->ctx_metal, "eval", ctx->buf_compute.addr, ctx->buf_compute.size, 0));
|
||||||
LLAMA_METAL_CHECK_BUF(ggml_metal_add_buffer(ctx->ctx_metal, "kv", ctx->kv_self.buf.addr, ctx->kv_self.buf.size, 0));
|
LLAMA_METAL_CHECK_BUF(ggml_metal_add_buffer(ctx->ctx_metal, "kv", ctx->kv_self.buf.addr, ctx->kv_self.buf.size, 0));
|
||||||
|
|
||||||
LLAMA_METAL_CHECK_BUF(ggml_metal_add_buffer(ctx->ctx_metal, "scr0", ctx->buf_scratch[0].addr, ctx->buf_scratch[0].size, 0));
|
LLAMA_METAL_CHECK_BUF(ggml_metal_add_buffer(ctx->ctx_metal, "alloc", ctx->buf_alloc.addr, ctx->buf_alloc.size, 0));
|
||||||
LLAMA_METAL_CHECK_BUF(ggml_metal_add_buffer(ctx->ctx_metal, "scr1", ctx->buf_scratch[1].addr, ctx->buf_scratch[1].size, 0));
|
|
||||||
#undef LLAMA_METAL_CHECK_BUF
|
#undef LLAMA_METAL_CHECK_BUF
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
@ -4193,6 +4295,10 @@ int llama_n_embd(const struct llama_context * ctx) {
|
||||||
return ctx->model.hparams.n_embd;
|
return ctx->model.hparams.n_embd;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int llama_model_type(const struct llama_model * model, char * buf, size_t buf_size) {
|
||||||
|
return snprintf(buf, buf_size, "LLaMA %s %s", llama_model_type_name(model->type), llama_ftype_name(model->hparams.ftype));
|
||||||
|
}
|
||||||
|
|
||||||
int llama_get_vocab_from_model(
|
int llama_get_vocab_from_model(
|
||||||
const struct llama_model * model,
|
const struct llama_model * model,
|
||||||
const char * * strings,
|
const char * * strings,
|
||||||
|
|
2
llama.h
2
llama.h
|
@ -351,6 +351,8 @@ extern "C" {
|
||||||
LLAMA_API int llama_n_ctx_from_model (const struct llama_model * model);
|
LLAMA_API int llama_n_ctx_from_model (const struct llama_model * model);
|
||||||
LLAMA_API int llama_n_embd_from_model (const struct llama_model * model);
|
LLAMA_API int llama_n_embd_from_model (const struct llama_model * model);
|
||||||
|
|
||||||
|
LLAMA_API int llama_model_type(const struct llama_model * model, char * buf, size_t buf_size);
|
||||||
|
|
||||||
// Get the vocabulary as output parameters.
|
// Get the vocabulary as output parameters.
|
||||||
// Returns number of results.
|
// Returns number of results.
|
||||||
LLAMA_API int llama_get_vocab(
|
LLAMA_API int llama_get_vocab(
|
||||||
|
|
403
tests/test-llama-grammar.cpp
Normal file
403
tests/test-llama-grammar.cpp
Normal file
|
@ -0,0 +1,403 @@
|
||||||
|
#ifdef NDEBUG
|
||||||
|
#undef NDEBUG
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#include "llama.cpp"
|
||||||
|
#include "examples/common.cpp"
|
||||||
|
#include "examples/grammar-parser.cpp"
|
||||||
|
#include <cassert>
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
grammar_parser::parse_state parsed_grammar;
|
||||||
|
|
||||||
|
std::vector<std::pair<std::string, uint32_t>> expected = {
|
||||||
|
{"expr", 2},
|
||||||
|
{"expr_6", 6},
|
||||||
|
{"expr_7", 7},
|
||||||
|
{"ident", 8},
|
||||||
|
{"ident_10", 10},
|
||||||
|
{"num", 9},
|
||||||
|
{"num_11", 11},
|
||||||
|
{"root", 0},
|
||||||
|
{"root_1", 1},
|
||||||
|
{"root_5", 5},
|
||||||
|
{"term", 4},
|
||||||
|
{"ws", 3},
|
||||||
|
{"ws_12", 12},
|
||||||
|
};
|
||||||
|
|
||||||
|
std::vector<std::vector<llama_grammar_element>> expected_rules = {
|
||||||
|
{{LLAMA_GRETYPE_RULE_REF, 5}, {LLAMA_GRETYPE_END, 0}},
|
||||||
|
{
|
||||||
|
{LLAMA_GRETYPE_RULE_REF, 2},
|
||||||
|
{LLAMA_GRETYPE_CHAR, 61},
|
||||||
|
{LLAMA_GRETYPE_RULE_REF, 3},
|
||||||
|
{LLAMA_GRETYPE_RULE_REF, 4},
|
||||||
|
{LLAMA_GRETYPE_CHAR, 10},
|
||||||
|
{LLAMA_GRETYPE_END, 0},
|
||||||
|
},
|
||||||
|
{{LLAMA_GRETYPE_RULE_REF, 4}, {LLAMA_GRETYPE_RULE_REF, 7}, {LLAMA_GRETYPE_END, 0}},
|
||||||
|
{{LLAMA_GRETYPE_RULE_REF, 12}, {LLAMA_GRETYPE_END, 0}},
|
||||||
|
{
|
||||||
|
{LLAMA_GRETYPE_RULE_REF, 8},
|
||||||
|
{LLAMA_GRETYPE_ALT, 0},
|
||||||
|
{LLAMA_GRETYPE_RULE_REF, 9},
|
||||||
|
{LLAMA_GRETYPE_ALT, 0},
|
||||||
|
{LLAMA_GRETYPE_CHAR, 40},
|
||||||
|
{LLAMA_GRETYPE_RULE_REF, 3},
|
||||||
|
{LLAMA_GRETYPE_RULE_REF, 2},
|
||||||
|
{LLAMA_GRETYPE_CHAR, 41},
|
||||||
|
{LLAMA_GRETYPE_RULE_REF, 3},
|
||||||
|
{LLAMA_GRETYPE_END, 0},
|
||||||
|
},
|
||||||
|
{{LLAMA_GRETYPE_RULE_REF, 1}, {LLAMA_GRETYPE_RULE_REF, 5}, {LLAMA_GRETYPE_ALT, 0}, {LLAMA_GRETYPE_RULE_REF, 1}, {LLAMA_GRETYPE_END, 0}},
|
||||||
|
{
|
||||||
|
{LLAMA_GRETYPE_CHAR, 45},
|
||||||
|
{LLAMA_GRETYPE_CHAR_ALT, 43},
|
||||||
|
{LLAMA_GRETYPE_CHAR_ALT, 42},
|
||||||
|
{LLAMA_GRETYPE_CHAR_ALT, 47},
|
||||||
|
{LLAMA_GRETYPE_RULE_REF, 4},
|
||||||
|
{LLAMA_GRETYPE_END, 0},
|
||||||
|
},
|
||||||
|
{{LLAMA_GRETYPE_RULE_REF, 6}, {LLAMA_GRETYPE_RULE_REF, 7}, {LLAMA_GRETYPE_ALT, 0}, {LLAMA_GRETYPE_END, 0}},
|
||||||
|
{
|
||||||
|
{LLAMA_GRETYPE_CHAR, 97},
|
||||||
|
{LLAMA_GRETYPE_CHAR_RNG_UPPER, 122},
|
||||||
|
{LLAMA_GRETYPE_RULE_REF, 10},
|
||||||
|
{LLAMA_GRETYPE_RULE_REF, 3},
|
||||||
|
{LLAMA_GRETYPE_END, 0},
|
||||||
|
},
|
||||||
|
{{LLAMA_GRETYPE_RULE_REF, 11}, {LLAMA_GRETYPE_RULE_REF, 3}, {LLAMA_GRETYPE_END, 0}},
|
||||||
|
{
|
||||||
|
{LLAMA_GRETYPE_CHAR, 97},
|
||||||
|
{LLAMA_GRETYPE_CHAR_RNG_UPPER, 122},
|
||||||
|
{LLAMA_GRETYPE_CHAR_ALT, 48},
|
||||||
|
{LLAMA_GRETYPE_CHAR_RNG_UPPER, 57},
|
||||||
|
{LLAMA_GRETYPE_CHAR_ALT, 95},
|
||||||
|
{LLAMA_GRETYPE_RULE_REF, 10},
|
||||||
|
{LLAMA_GRETYPE_ALT, 0},
|
||||||
|
{LLAMA_GRETYPE_END, 0},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
{LLAMA_GRETYPE_CHAR, 48},
|
||||||
|
{LLAMA_GRETYPE_CHAR_RNG_UPPER, 57},
|
||||||
|
{LLAMA_GRETYPE_RULE_REF, 11},
|
||||||
|
{LLAMA_GRETYPE_ALT, 0},
|
||||||
|
{LLAMA_GRETYPE_CHAR, 48},
|
||||||
|
{LLAMA_GRETYPE_CHAR_RNG_UPPER, 57},
|
||||||
|
{LLAMA_GRETYPE_END, 0},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
{LLAMA_GRETYPE_CHAR, 32},
|
||||||
|
{LLAMA_GRETYPE_CHAR_ALT, 9},
|
||||||
|
{LLAMA_GRETYPE_CHAR_ALT, 10},
|
||||||
|
{LLAMA_GRETYPE_RULE_REF, 12},
|
||||||
|
{LLAMA_GRETYPE_ALT, 0},
|
||||||
|
{LLAMA_GRETYPE_END, 0},
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
for (auto pair : expected)
|
||||||
|
{
|
||||||
|
parsed_grammar.symbol_ids[pair.first] = pair.second;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (auto rule : expected_rules)
|
||||||
|
{
|
||||||
|
parsed_grammar.rules.push_back({});
|
||||||
|
for (auto element : rule)
|
||||||
|
{
|
||||||
|
parsed_grammar.rules.back().push_back(element);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
llama_grammar *grammar = NULL;
|
||||||
|
std::vector<const llama_grammar_element *> grammar_rules(parsed_grammar.c_rules());
|
||||||
|
grammar = llama_grammar_init(
|
||||||
|
grammar_rules.data(), grammar_rules.size(), parsed_grammar.symbol_ids.at("root"));
|
||||||
|
|
||||||
|
std::vector<std::vector<llama_grammar_element>> expected_stacks = {
|
||||||
|
{
|
||||||
|
{LLAMA_GRETYPE_RULE_REF, 5},
|
||||||
|
{LLAMA_GRETYPE_CHAR, 61},
|
||||||
|
{LLAMA_GRETYPE_RULE_REF, 7},
|
||||||
|
{LLAMA_GRETYPE_CHAR, 97},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
{LLAMA_GRETYPE_RULE_REF, 5},
|
||||||
|
{LLAMA_GRETYPE_CHAR, 61},
|
||||||
|
{LLAMA_GRETYPE_RULE_REF, 7},
|
||||||
|
{LLAMA_GRETYPE_RULE_REF, 3},
|
||||||
|
{LLAMA_GRETYPE_CHAR, 48},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
{LLAMA_GRETYPE_RULE_REF, 5},
|
||||||
|
{LLAMA_GRETYPE_CHAR, 61},
|
||||||
|
{LLAMA_GRETYPE_RULE_REF, 7},
|
||||||
|
{LLAMA_GRETYPE_RULE_REF, 3},
|
||||||
|
{LLAMA_GRETYPE_CHAR, 48},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
{LLAMA_GRETYPE_RULE_REF, 5},
|
||||||
|
{LLAMA_GRETYPE_CHAR, 61},
|
||||||
|
{LLAMA_GRETYPE_RULE_REF, 7},
|
||||||
|
{LLAMA_GRETYPE_CHAR, 40},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
{LLAMA_GRETYPE_CHAR, 61},
|
||||||
|
{LLAMA_GRETYPE_RULE_REF, 7},
|
||||||
|
{LLAMA_GRETYPE_CHAR, 97},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
{LLAMA_GRETYPE_CHAR, 61},
|
||||||
|
{LLAMA_GRETYPE_RULE_REF, 7},
|
||||||
|
{LLAMA_GRETYPE_RULE_REF, 3},
|
||||||
|
{LLAMA_GRETYPE_CHAR, 48},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
{LLAMA_GRETYPE_CHAR, 61},
|
||||||
|
{LLAMA_GRETYPE_RULE_REF, 7},
|
||||||
|
{LLAMA_GRETYPE_RULE_REF, 3},
|
||||||
|
{LLAMA_GRETYPE_CHAR, 48},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
{LLAMA_GRETYPE_CHAR, 61},
|
||||||
|
{LLAMA_GRETYPE_RULE_REF, 7},
|
||||||
|
{LLAMA_GRETYPE_CHAR, 40},
|
||||||
|
}};
|
||||||
|
|
||||||
|
auto index = 0;
|
||||||
|
for (auto stack : grammar->stacks)
|
||||||
|
{
|
||||||
|
// compare stack to expected_stack
|
||||||
|
for (uint32_t i = 0; i < stack.size(); i++)
|
||||||
|
{
|
||||||
|
auto element = stack[i];
|
||||||
|
auto expected_element = expected_stacks[index][i];
|
||||||
|
|
||||||
|
// pretty print error message before asserting
|
||||||
|
if (expected_element.type != element->type || expected_element.value != element->value)
|
||||||
|
{
|
||||||
|
fprintf(stderr, "index: %d\n", index);
|
||||||
|
fprintf(stderr, "expected_element: %d, %d\n", expected_element.type, expected_element.value);
|
||||||
|
fprintf(stderr, "actual_element: %d, %d\n", element->type, element->value);
|
||||||
|
fprintf(stderr, "expected_element != actual_element\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
assert(expected_element.type == element->type && expected_element.value == element->value);
|
||||||
|
}
|
||||||
|
index++;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::vector<std::vector<const llama_grammar_element *>> next_stacks;
|
||||||
|
std::vector<llama_grammar_candidate> next_candidates;
|
||||||
|
next_candidates.resize(24);
|
||||||
|
|
||||||
|
for (size_t i = 0; i < 24; ++i)
|
||||||
|
{
|
||||||
|
uint32_t *cp = new uint32_t[2]; // dynamically allocate memory for code_point
|
||||||
|
cp[0] = 37 + i;
|
||||||
|
cp[1] = 0;
|
||||||
|
next_candidates[i] = {i, cp, {}};
|
||||||
|
}
|
||||||
|
|
||||||
|
std::vector<std::vector<std::pair<uint32_t, uint16_t>>> expected_reject = {
|
||||||
|
{
|
||||||
|
{0, 37},
|
||||||
|
{1, 38},
|
||||||
|
{2, 39},
|
||||||
|
{3, 40},
|
||||||
|
{4, 41},
|
||||||
|
{5, 42},
|
||||||
|
{6, 43},
|
||||||
|
{7, 44},
|
||||||
|
{8, 45},
|
||||||
|
{9, 46},
|
||||||
|
{10, 47},
|
||||||
|
{11, 48},
|
||||||
|
{12, 49},
|
||||||
|
{13, 50},
|
||||||
|
{14, 51},
|
||||||
|
{15, 52},
|
||||||
|
{16, 53},
|
||||||
|
{17, 54},
|
||||||
|
{18, 55},
|
||||||
|
{19, 56},
|
||||||
|
{20, 57},
|
||||||
|
{21, 58},
|
||||||
|
{22, 59},
|
||||||
|
{23, 60},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
{0, 37},
|
||||||
|
{1, 38},
|
||||||
|
{2, 39},
|
||||||
|
{3, 40},
|
||||||
|
{4, 41},
|
||||||
|
{5, 42},
|
||||||
|
{6, 43},
|
||||||
|
{7, 44},
|
||||||
|
{8, 45},
|
||||||
|
{9, 46},
|
||||||
|
{10, 47},
|
||||||
|
{21, 58},
|
||||||
|
{22, 59},
|
||||||
|
{23, 60},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
{0, 37},
|
||||||
|
{1, 38},
|
||||||
|
{2, 39},
|
||||||
|
{3, 40},
|
||||||
|
{4, 41},
|
||||||
|
{5, 42},
|
||||||
|
{6, 43},
|
||||||
|
{7, 44},
|
||||||
|
{8, 45},
|
||||||
|
{9, 46},
|
||||||
|
{10, 47},
|
||||||
|
{21, 58},
|
||||||
|
{22, 59},
|
||||||
|
{23, 60},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
{0, 37},
|
||||||
|
{1, 38},
|
||||||
|
{2, 39},
|
||||||
|
{4, 41},
|
||||||
|
{5, 42},
|
||||||
|
{6, 43},
|
||||||
|
{7, 44},
|
||||||
|
{8, 45},
|
||||||
|
{9, 46},
|
||||||
|
{10, 47},
|
||||||
|
{11, 48},
|
||||||
|
{12, 49},
|
||||||
|
{13, 50},
|
||||||
|
{14, 51},
|
||||||
|
{15, 52},
|
||||||
|
{16, 53},
|
||||||
|
{17, 54},
|
||||||
|
{18, 55},
|
||||||
|
{19, 56},
|
||||||
|
{20, 57},
|
||||||
|
{21, 58},
|
||||||
|
{22, 59},
|
||||||
|
{23, 60},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
{0, 37},
|
||||||
|
{1, 38},
|
||||||
|
{2, 39},
|
||||||
|
{3, 40},
|
||||||
|
{4, 41},
|
||||||
|
{5, 42},
|
||||||
|
{6, 43},
|
||||||
|
{7, 44},
|
||||||
|
{8, 45},
|
||||||
|
{9, 46},
|
||||||
|
{10, 47},
|
||||||
|
{11, 48},
|
||||||
|
{12, 49},
|
||||||
|
{13, 50},
|
||||||
|
{14, 51},
|
||||||
|
{15, 52},
|
||||||
|
{16, 53},
|
||||||
|
{17, 54},
|
||||||
|
{18, 55},
|
||||||
|
{19, 56},
|
||||||
|
{20, 57},
|
||||||
|
{21, 58},
|
||||||
|
{22, 59},
|
||||||
|
{23, 60},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
{0, 37},
|
||||||
|
{1, 38},
|
||||||
|
{2, 39},
|
||||||
|
{3, 40},
|
||||||
|
{4, 41},
|
||||||
|
{5, 42},
|
||||||
|
{6, 43},
|
||||||
|
{7, 44},
|
||||||
|
{8, 45},
|
||||||
|
{9, 46},
|
||||||
|
{10, 47},
|
||||||
|
{21, 58},
|
||||||
|
{22, 59},
|
||||||
|
{23, 60},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
{0, 37},
|
||||||
|
{1, 38},
|
||||||
|
{2, 39},
|
||||||
|
{3, 40},
|
||||||
|
{4, 41},
|
||||||
|
{5, 42},
|
||||||
|
{6, 43},
|
||||||
|
{7, 44},
|
||||||
|
{8, 45},
|
||||||
|
{9, 46},
|
||||||
|
{10, 47},
|
||||||
|
{21, 58},
|
||||||
|
{22, 59},
|
||||||
|
{23, 60},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
{0, 37},
|
||||||
|
{1, 38},
|
||||||
|
{2, 39},
|
||||||
|
{4, 41},
|
||||||
|
{5, 42},
|
||||||
|
{6, 43},
|
||||||
|
{7, 44},
|
||||||
|
{8, 45},
|
||||||
|
{9, 46},
|
||||||
|
{10, 47},
|
||||||
|
{11, 48},
|
||||||
|
{12, 49},
|
||||||
|
{13, 50},
|
||||||
|
{14, 51},
|
||||||
|
{15, 52},
|
||||||
|
{16, 53},
|
||||||
|
{17, 54},
|
||||||
|
{18, 55},
|
||||||
|
{19, 56},
|
||||||
|
{20, 57},
|
||||||
|
{21, 58},
|
||||||
|
{22, 59},
|
||||||
|
{23, 60},
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
std::vector<llama_grammar_candidate> rejects = llama_grammar_reject_candidates_for_stack(grammar->rules, grammar->stacks[0], next_candidates);
|
||||||
|
|
||||||
|
std::vector<std::vector<llama_grammar_candidate>> all_rejects;
|
||||||
|
|
||||||
|
for (std::size_t count = 0; count < grammar->stacks.size(); ++count)
|
||||||
|
{
|
||||||
|
rejects = llama_grammar_reject_candidates_for_stack(grammar->rules, grammar->stacks[count], next_candidates);
|
||||||
|
all_rejects.push_back(rejects);
|
||||||
|
}
|
||||||
|
|
||||||
|
index = 0;
|
||||||
|
for (auto rej : all_rejects)
|
||||||
|
{
|
||||||
|
for (uint32_t i = 0; i < rej.size(); i++)
|
||||||
|
{
|
||||||
|
auto element = rej[i];
|
||||||
|
auto expected_element = expected_reject[index][i];
|
||||||
|
assert(element.index == expected_element.first && *element.code_points == expected_element.second);
|
||||||
|
}
|
||||||
|
index++;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (auto &candidate : next_candidates)
|
||||||
|
{
|
||||||
|
delete[] candidate.code_points;
|
||||||
|
candidate.code_points = nullptr;
|
||||||
|
}
|
||||||
|
delete grammar;
|
||||||
|
return 0;
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue