Skip to content

Commit 9a735ae

Browse files
committed
examplse : de-shadow
ggml-ci
1 parent 82caffa commit 9a735ae

File tree

16 files changed

+152
-159
lines changed

16 files changed

+152
-159
lines changed

common/arg.cpp

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,19 @@
1717

1818
using json = nlohmann::ordered_json;
1919

20-
common_arg & common_arg::set_examples(std::initializer_list<enum llama_example> examples) {
21-
this->examples = std::move(examples);
20+
common_arg & common_arg::set_examples(std::initializer_list<enum llama_example> vals) {
21+
examples = std::move(vals);
2222
return *this;
2323
}
2424

25-
common_arg & common_arg::set_excludes(std::initializer_list<enum llama_example> excludes) {
26-
this->excludes = std::move(excludes);
25+
common_arg & common_arg::set_excludes(std::initializer_list<enum llama_example> vals) {
26+
excludes = std::move(vals);
2727
return *this;
2828
}
2929

30-
common_arg & common_arg::set_env(const char * env) {
31-
help = help + "\n(env: " + env + ")";
32-
this->env = env;
30+
common_arg & common_arg::set_env(const char * val) {
31+
help = help + "\n(env: " + val + ")";
32+
env = val;
3333
return *this;
3434
}
3535

@@ -46,8 +46,10 @@ bool common_arg::is_exclude(enum llama_example ex) {
4646
return excludes.find(ex) != excludes.end();
4747
}
4848

49-
bool common_arg::get_value_from_env(std::string & output) {
50-
if (env == nullptr) return false;
49+
bool common_arg::get_value_from_env(std::string & output) const {
50+
if (env == nullptr) {
51+
return false;
52+
}
5153
char * value = std::getenv(env);
5254
if (value) {
5355
output = value;
@@ -56,7 +58,7 @@ bool common_arg::get_value_from_env(std::string & output) {
5658
return false;
5759
}
5860

59-
bool common_arg::has_value_from_env() {
61+
bool common_arg::has_value_from_env() const {
6062
return env != nullptr && std::getenv(env);
6163
}
6264

@@ -87,7 +89,7 @@ static std::vector<std::string> break_str_into_lines(std::string input, size_t m
8789
return result;
8890
}
8991

90-
std::string common_arg::to_string() {
92+
std::string common_arg::to_string() const {
9193
// params for printing to console
9294
const static int n_leading_spaces = 40;
9395
const static int n_char_per_line_help = 70; // TODO: detect this based on current console
@@ -192,8 +194,6 @@ static std::string get_all_kv_cache_types() {
192194
//
193195

194196
static bool common_params_parse_ex(int argc, char ** argv, common_params_context & ctx_arg) {
195-
std::string arg;
196-
const std::string arg_prefix = "--";
197197
common_params & params = ctx_arg.params;
198198

199199
std::unordered_map<std::string, common_arg *> arg_to_options;

common/arg.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,15 +53,15 @@ struct common_arg {
5353
void (*handler)(common_params & params, const std::string &, const std::string &)
5454
) : args(args), value_hint(value_hint), value_hint_2(value_hint_2), help(help), handler_str_str(handler) {}
5555

56-
common_arg & set_examples(std::initializer_list<enum llama_example> examples);
57-
common_arg & set_excludes(std::initializer_list<enum llama_example> excludes);
58-
common_arg & set_env(const char * env);
56+
common_arg & set_examples(std::initializer_list<enum llama_example> vals);
57+
common_arg & set_excludes(std::initializer_list<enum llama_example> vals);
58+
common_arg & set_env(const char * val);
5959
common_arg & set_sparam();
6060
bool in_example(enum llama_example ex);
6161
bool is_exclude(enum llama_example ex);
62-
bool get_value_from_env(std::string & output);
63-
bool has_value_from_env();
64-
std::string to_string();
62+
bool get_value_from_env(std::string & output) const;
63+
bool has_value_from_env() const;
64+
std::string to_string() const;
6565
};
6666

6767
struct common_params_context {

common/common.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -763,9 +763,11 @@ bool fs_create_directory_with_parents(const std::string & path) {
763763
return true;
764764
#else
765765
// if the path already exists, check whether it's a directory
766-
struct stat info;
767-
if (stat(path.c_str(), &info) == 0) {
768-
return S_ISDIR(info.st_mode);
766+
{
767+
struct stat info;
768+
if (stat(path.c_str(), &info) == 0) {
769+
return S_ISDIR(info.st_mode);
770+
}
769771
}
770772

771773
size_t pos_slash = 1; // skip leading slashes for directory creation
@@ -796,7 +798,7 @@ bool fs_create_directory_with_parents(const std::string & path) {
796798
}
797799

798800
std::string fs_get_cache_directory() {
799-
std::string cache_directory = "";
801+
std::string cache_directory;
800802
auto ensure_trailing_slash = [](std::string p) {
801803
// Make sure to add trailing slash
802804
if (p.back() != DIRECTORY_SEPARATOR) {

common/console.cpp

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ namespace console {
4343
static bool simple_io = true;
4444
static display_t current_display = reset;
4545

46-
static FILE* out = stdout;
46+
static FILE* fout = stdout;
4747

4848
#if defined (_WIN32)
4949
static void* hConsole;
@@ -110,7 +110,7 @@ namespace console {
110110

111111
tty = fopen("/dev/tty", "w+");
112112
if (tty != nullptr) {
113-
out = tty;
113+
fout = tty;
114114
}
115115
}
116116

@@ -126,7 +126,7 @@ namespace console {
126126
// Restore settings on POSIX systems
127127
if (!simple_io) {
128128
if (tty != nullptr) {
129-
out = stdout;
129+
fout = stdout;
130130
fclose(tty);
131131
tty = nullptr;
132132
}
@@ -145,19 +145,19 @@ namespace console {
145145
fflush(stdout);
146146
switch(display) {
147147
case reset:
148-
fprintf(out, ANSI_COLOR_RESET);
148+
fprintf(fout, ANSI_COLOR_RESET);
149149
break;
150150
case prompt:
151-
fprintf(out, ANSI_COLOR_YELLOW);
151+
fprintf(fout, ANSI_COLOR_YELLOW);
152152
break;
153153
case user_input:
154-
fprintf(out, ANSI_BOLD ANSI_COLOR_GREEN);
154+
fprintf(fout, ANSI_BOLD ANSI_COLOR_GREEN);
155155
break;
156156
case error:
157-
fprintf(out, ANSI_BOLD ANSI_COLOR_RED);
157+
fprintf(fout, ANSI_BOLD ANSI_COLOR_RED);
158158
}
159159
current_display = display;
160-
fflush(out);
160+
fflush(fout);
161161
}
162162
}
163163

@@ -233,7 +233,7 @@ namespace console {
233233
return;
234234
}
235235
#endif
236-
putc('\b', out);
236+
putc('\b', fout);
237237
}
238238

239239
static int estimateWidth(char32_t codepoint) {
@@ -274,7 +274,7 @@ namespace console {
274274
#else
275275
// We can trust expectedWidth if we've got one
276276
if (expectedWidth >= 0 || tty == nullptr) {
277-
fwrite(utf8_codepoint, length, 1, out);
277+
fwrite(utf8_codepoint, length, 1, fout);
278278
return expectedWidth;
279279
}
280280

@@ -311,7 +311,7 @@ namespace console {
311311
pop_cursor();
312312
put_codepoint(&ch, 1, 1);
313313
#else
314-
fprintf(out, "\b%c", ch);
314+
fprintf(fout, "\b%c", ch);
315315
#endif
316316
}
317317

@@ -353,7 +353,7 @@ namespace console {
353353
}
354354

355355
static bool readline_advanced(std::string & line, bool multiline_input) {
356-
if (out != stdout) {
356+
if (fout != stdout) {
357357
fflush(stdout);
358358
}
359359

@@ -364,7 +364,7 @@ namespace console {
364364

365365
char32_t input_char;
366366
while (true) {
367-
fflush(out); // Ensure all output is displayed before waiting for input
367+
fflush(fout); // Ensure all output is displayed before waiting for input
368368
input_char = getchar32();
369369

370370
if (input_char == '\r' || input_char == '\n') {
@@ -432,7 +432,7 @@ namespace console {
432432
line.pop_back();
433433
if (last == '\\') {
434434
line += '\n';
435-
fputc('\n', out);
435+
fputc('\n', fout);
436436
has_more = !has_more;
437437
} else {
438438
// llama will just eat the single space, it won't act as a space
@@ -447,11 +447,11 @@ namespace console {
447447
has_more = false;
448448
} else {
449449
line += '\n';
450-
fputc('\n', out);
450+
fputc('\n', fout);
451451
}
452452
}
453453

454-
fflush(out);
454+
fflush(fout);
455455
return has_more;
456456
}
457457

common/log.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -338,16 +338,16 @@ struct common_log {
338338
resume();
339339
}
340340

341-
void set_prefix(bool prefix) {
341+
void set_prefix(bool val) {
342342
std::lock_guard<std::mutex> lock(mtx);
343343

344-
this->prefix = prefix;
344+
prefix = val;
345345
}
346346

347-
void set_timestamps(bool timestamps) {
347+
void set_timestamps(bool val) {
348348
std::lock_guard<std::mutex> lock(mtx);
349349

350-
this->timestamps = timestamps;
350+
timestamps = val;
351351
}
352352
};
353353

examples/convert-llama2c-to-ggml/convert-llama2c-to-ggml.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -471,12 +471,12 @@ struct my_llama_file {
471471
GGML_ASSERT(ret == 0); // same
472472
}
473473

474-
void read_raw(void * ptr, size_t size) {
475-
if (size == 0) {
474+
void read_raw(void * ptr, size_t size_cur) {
475+
if (size_cur == 0) {
476476
return;
477477
}
478478
errno = 0;
479-
std::size_t ret = std::fread(ptr, size, 1, fp);
479+
std::size_t ret = std::fread(ptr, size_cur, 1, fp);
480480
if (ferror(fp)) {
481481
die_fmt("fread failed: %s", strerror(errno));
482482
}

examples/gbnf-validator/gbnf-validator.cpp

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -60,13 +60,6 @@ int main(int argc, char** argv) {
6060
const std::string grammar_filename = argv[1];
6161
const std::string input_filename = argv[2];
6262

63-
// Read the GBNF grammar file
64-
FILE* grammar_file = fopen(grammar_filename.c_str(), "r");
65-
if (!grammar_file) {
66-
fprintf(stdout, "Failed to open grammar file: %s\n", grammar_filename.c_str());
67-
return 1;
68-
}
69-
7063
std::string grammar_str;
7164
{
7265
std::ifstream grammar_file(grammar_filename);

examples/imatrix/imatrix.cpp

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,7 @@ void IMatrixCollector::save_imatrix(int ncall) const {
294294
bool IMatrixCollector::load_imatrix(const char * fname) {
295295
std::ifstream in(fname, std::ios::binary);
296296
if (!in) {
297-
LOG_ERR("%s: failed to open %s\n",__func__, fname);
297+
LOG_ERR("%s: failed to open %s\n", __func__, fname);
298298
return false;
299299
}
300300
int n_entries;
@@ -308,7 +308,7 @@ bool IMatrixCollector::load_imatrix(const char * fname) {
308308
std::vector<char> name_as_vec(len+1);
309309
in.read((char *)name_as_vec.data(), len);
310310
if (in.fail()) {
311-
LOG_ERR("%s: failed reading name for entry %d from %s\n",__func__,i+1, fname);
311+
LOG_ERR("%s: failed reading name for entry %d from %s\n", __func__, i + 1, fname);
312312
return false;
313313
}
314314
name_as_vec[len] = 0;
@@ -319,7 +319,7 @@ bool IMatrixCollector::load_imatrix(const char * fname) {
319319
int nval;
320320
in.read((char *)&nval, sizeof(nval));
321321
if (in.fail() || nval < 1) {
322-
LOG_ERR("%s: failed reading number of values for entry %d\n",__func__,i);
322+
LOG_ERR("%s: failed reading number of values for entry %d\n", __func__, i);
323323
m_stats = {};
324324
return false;
325325
}
@@ -332,15 +332,15 @@ bool IMatrixCollector::load_imatrix(const char * fname) {
332332
std::vector<float> tmp(nval);
333333
in.read((char*)tmp.data(), nval*sizeof(float));
334334
if (in.fail()) {
335-
LOG_ERR("%s: failed reading data for entry %d\n",__func__,i);
335+
LOG_ERR("%s: failed reading data for entry %d\n", __func__, i);
336336
m_stats = {};
337337
return false;
338338
}
339339

340340
// Recreate the state as expected by save_imatrix(), and corerct for weighted sum.
341-
for (int i = 0; i < nval; i++) {
342-
e.values[i] += tmp[i];
343-
e.counts[i] += ncall;
341+
for (int j = 0; j < nval; j++) {
342+
e.values[j] += tmp[j];
343+
e.counts[j] += ncall;
344344
}
345345
e.ncall += ncall;
346346

@@ -488,12 +488,10 @@ static bool compute_imatrix(llama_context * ctx, const common_params & params) {
488488
logits.reserve((size_t)n_ctx * n_vocab);
489489
}
490490

491-
for (int i = 0; i < n_chunk; ++i) {
492-
const int start = i * n_ctx;
491+
for (int ich = 0; ich < n_chunk; ++ich) {
492+
const int start = ich * n_ctx;
493493
const int end = start + n_ctx;
494494

495-
std::vector<float> logits;
496-
497495
const auto t_start = std::chrono::high_resolution_clock::now();
498496

499497
// clear the KV cache
@@ -537,7 +535,7 @@ static bool compute_imatrix(llama_context * ctx, const common_params & params) {
537535

538536
const auto t_end = std::chrono::high_resolution_clock::now();
539537

540-
if (i == 0) {
538+
if (ich == 0) {
541539
const float t_total = std::chrono::duration<float>(t_end - t_start).count();
542540
LOG_INF("%s: %.2f seconds per pass - ETA ", __func__, t_total);
543541
int total_seconds = (int)(t_total * n_chunk);
@@ -555,7 +553,7 @@ static bool compute_imatrix(llama_context * ctx, const common_params & params) {
555553
workers, nll, nll2, logit_history.data() + start + first, prob_history.data() + start + first);
556554
count += n_ctx - first - 1;
557555

558-
LOG("[%d]%.4lf,", i + 1, std::exp(nll / count));
556+
LOG("[%d]%.4lf,", ich + 1, std::exp(nll / count));
559557
fflush(stdout);
560558

561559
logits.clear();

examples/infill/infill.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -462,14 +462,14 @@ int main(int argc, char ** argv) {
462462
}
463463

464464
// tokenize new prefix and suffix
465-
std::vector<llama_token> inp_pfx = common_tokenize(ctx, params.input_prefix, false);
466-
std::vector<llama_token> inp_sfx = common_tokenize(ctx, params.input_suffix, false);
465+
std::vector<llama_token> inp_pfx_cur = common_tokenize(ctx, params.input_prefix, false);
466+
std::vector<llama_token> inp_sfx_cur = common_tokenize(ctx, params.input_suffix, false);
467467

468-
inp_pfx.insert(inp_pfx.begin(), llama_vocab_fim_pre(vocab));
469-
inp_sfx.insert(inp_sfx.begin(), llama_vocab_fim_suf(vocab));
468+
inp_pfx_cur.insert(inp_pfx_cur.begin(), llama_vocab_fim_pre(vocab));
469+
inp_sfx_cur.insert(inp_sfx_cur.begin(), llama_vocab_fim_suf(vocab));
470470

471-
embd_inp = params.spm_infill ? inp_sfx : inp_pfx;
472-
embd_end = params.spm_infill ? inp_pfx : inp_sfx;
471+
embd_inp = params.spm_infill ? inp_sfx_cur : inp_pfx_cur;
472+
embd_end = params.spm_infill ? inp_pfx_cur : inp_sfx_cur;
473473
if (add_bos) {
474474
embd_inp.insert(embd_inp.begin(), llama_vocab_bos(vocab));
475475
}

0 commit comments

Comments
 (0)