mirror of
https://github.com/ggerganov/llama.cpp.git
synced 2024-12-25 10:54:36 +00:00
main : allow empty --prompt-cache file (#5176)
* allow empty --prompt-cache file This allows the use of std::tmpnam(), std::tmpfile(), Python's tempfile.NamedTemporaryFile(), and similar create-empty-file API's for the user. I switched from the C fopen API to the C++ filesystem api to get around the fact that, to the best of my knowledge, C has no portable way to get the file size above LONG_MAX, with std::ftell() returning long? fallback to std::ifstream for c++ < 17 (the project is currently targeting C++11 it seems - file_exists() and file_size() can be removed when we upgrade to c++17) * formatting (requested in codereview) * remove c++17, file_is_empty
This commit is contained in:
parent
5589921ef8
commit
813416991a
@ -39,6 +39,17 @@ static std::ostringstream * g_output_ss;
|
|||||||
static std::vector<llama_token> * g_output_tokens;
|
static std::vector<llama_token> * g_output_tokens;
|
||||||
static bool is_interacting = false;
|
static bool is_interacting = false;
|
||||||
|
|
||||||
|
static bool file_exists(const std::string &path) {
|
||||||
|
std::ifstream f(path.c_str());
|
||||||
|
return f.good();
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool file_is_empty(const std::string &path) {
|
||||||
|
std::ifstream f;
|
||||||
|
f.exceptions(std::ifstream::failbit | std::ifstream::badbit);
|
||||||
|
f.open(path.c_str(), std::ios::in | std::ios::binary | std::ios::ate);
|
||||||
|
return f.tellg() == 0;
|
||||||
|
}
|
||||||
|
|
||||||
static void write_logfile(
|
static void write_logfile(
|
||||||
const llama_context * ctx, const gpt_params & params, const llama_model * model,
|
const llama_context * ctx, const gpt_params & params, const llama_model * model,
|
||||||
@ -215,12 +226,12 @@ int main(int argc, char ** argv) {
|
|||||||
|
|
||||||
if (!path_session.empty()) {
|
if (!path_session.empty()) {
|
||||||
LOG_TEE("%s: attempting to load saved session from '%s'\n", __func__, path_session.c_str());
|
LOG_TEE("%s: attempting to load saved session from '%s'\n", __func__, path_session.c_str());
|
||||||
|
if (!file_exists(path_session)) {
|
||||||
// fopen to check for existing session
|
LOG_TEE("%s: session file does not exist, will create.\n", __func__);
|
||||||
FILE * fp = std::fopen(path_session.c_str(), "rb");
|
} else if (file_is_empty(path_session)) {
|
||||||
if (fp != NULL) {
|
LOG_TEE("%s: The session file is empty. A new session will be initialized.\n", __func__);
|
||||||
std::fclose(fp);
|
} else {
|
||||||
|
// The file exists and is not empty
|
||||||
session_tokens.resize(n_ctx);
|
session_tokens.resize(n_ctx);
|
||||||
size_t n_token_count_out = 0;
|
size_t n_token_count_out = 0;
|
||||||
if (!llama_load_session_file(ctx, path_session.c_str(), session_tokens.data(), session_tokens.capacity(), &n_token_count_out)) {
|
if (!llama_load_session_file(ctx, path_session.c_str(), session_tokens.data(), session_tokens.capacity(), &n_token_count_out)) {
|
||||||
@ -229,10 +240,7 @@ int main(int argc, char ** argv) {
|
|||||||
}
|
}
|
||||||
session_tokens.resize(n_token_count_out);
|
session_tokens.resize(n_token_count_out);
|
||||||
llama_set_rng_seed(ctx, params.seed);
|
llama_set_rng_seed(ctx, params.seed);
|
||||||
|
LOG_TEE("%s: loaded a session with prompt size of %d tokens\n", __func__, (int)session_tokens.size());
|
||||||
LOG_TEE("%s: loaded a session with prompt size of %d tokens\n", __func__, (int) session_tokens.size());
|
|
||||||
} else {
|
|
||||||
LOG_TEE("%s: session file does not exist, will create\n", __func__);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user