Skip to content

Commit 745827a

Browse files
Dobiasdclaude
andcommitted
Apply clang-format to the GPT-2 chat example
No functional changes; satisfies the project's formatting CI check. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent 84c0a92 commit 745827a

4 files changed

Lines changed: 120 additions & 71 deletions

File tree

examples/gpt2_chat/main.cpp

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,8 @@ int sample_logits(const std::vector<float>& logits, float temperature,
5353
cand.resize(top_k);
5454
}
5555
float m = cand[0].first;
56-
for (const auto& c : cand) m = std::max(m, c.first);
56+
for (const auto& c : cand)
57+
m = std::max(m, c.first);
5758
float total = 0.0f;
5859
for (auto& c : cand) {
5960
c.first = std::exp(c.first - m);
@@ -64,12 +65,13 @@ int sample_logits(const std::vector<float>& logits, float temperature,
6465
float acc = 0.0f;
6566
for (const auto& c : cand) {
6667
acc += c.first;
67-
if (acc >= pick) return c.second;
68+
if (acc >= pick)
69+
return c.second;
6870
}
6971
return cand.back().second;
7072
}
7173

72-
} // namespace
74+
} // namespace
7375

7476
int main(int argc, char** argv)
7577
{
@@ -108,12 +110,15 @@ int main(int argc, char** argv)
108110
std::string line;
109111
while (true) {
110112
std::cout << "> " << std::flush;
111-
if (!std::getline(std::cin, line)) break;
112-
if (line.empty()) break;
113+
if (!std::getline(std::cin, line))
114+
break;
115+
if (line.empty())
116+
break;
113117

114118
gpt.reset();
115119
const auto prompt_ids = tok.encode(line);
116-
if (prompt_ids.empty()) continue;
120+
if (prompt_ids.empty())
121+
continue;
117122
if (prompt_ids.size() >= max_seq_len) {
118123
std::cerr << "[prompt is " << prompt_ids.size()
119124
<< " tokens; max_seq_len is " << max_seq_len
@@ -134,8 +139,10 @@ int main(int argc, char** argv)
134139
const int next_id = sample_logits(logits, temperature, top_k, rng);
135140
generated.push_back(next_id);
136141
std::cout << tok.decode({ next_id }) << std::flush;
137-
if (next_id == tok.eos_token_id()) break;
138-
if (gpt.cur_len() >= gpt.max_seq_len()) break;
142+
if (next_id == tok.eos_token_id())
143+
break;
144+
if (gpt.cur_len() >= gpt.max_seq_len())
145+
break;
139146
logits = gpt.step(next_id);
140147
}
141148
const auto t_gen1 = std::chrono::steady_clock::now();
@@ -144,9 +151,7 @@ int main(int argc, char** argv)
144151
<< std::chrono::duration<double>(t_pre1 - t_pre0).count() << " s, "
145152
<< "decode " << generated.size() << " tok "
146153
<< std::chrono::duration<double>(t_gen1 - t_gen0).count() << " s, "
147-
<< (generated.empty() ? 0.0 :
148-
1000.0 * std::chrono::duration<double>(t_gen1 - t_gen0).count()
149-
/ static_cast<double>(generated.size()))
154+
<< (generated.empty() ? 0.0 : 1000.0 * std::chrono::duration<double>(t_gen1 - t_gen0).count() / static_cast<double>(generated.size()))
150155
<< " ms/tok]\n";
151156
}
152157
return 0;

include/fdeep/llm/gpt2_bpe.hpp

Lines changed: 82 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ namespace llm {
7171
mapped[b] = true;
7272
}
7373
};
74-
add_range(0x21, 0x7E); // '!'..'~'
74+
add_range(0x21, 0x7E); // '!'..'~'
7575
add_range(0xA1, 0xAC);
7676
add_range(0xAE, 0xFF);
7777

@@ -159,14 +159,30 @@ namespace llm {
159159
if (text[i] == '\\' && i + 1 < n) {
160160
const char esc = text[++i];
161161
switch (esc) {
162-
case '"': out.push_back('"'); break;
163-
case '\\': out.push_back('\\'); break;
164-
case '/': out.push_back('/'); break;
165-
case 'b': out.push_back('\b'); break;
166-
case 'f': out.push_back('\f'); break;
167-
case 'n': out.push_back('\n'); break;
168-
case 'r': out.push_back('\r'); break;
169-
case 't': out.push_back('\t'); break;
162+
case '"':
163+
out.push_back('"');
164+
break;
165+
case '\\':
166+
out.push_back('\\');
167+
break;
168+
case '/':
169+
out.push_back('/');
170+
break;
171+
case 'b':
172+
out.push_back('\b');
173+
break;
174+
case 'f':
175+
out.push_back('\f');
176+
break;
177+
case 'n':
178+
out.push_back('\n');
179+
break;
180+
case 'r':
181+
out.push_back('\r');
182+
break;
183+
case 't':
184+
out.push_back('\t');
185+
break;
170186
case 'u': {
171187
if (i + 4 >= n) {
172188
throw std::runtime_error("vocab.json: bad \\u escape");
@@ -175,10 +191,14 @@ namespace llm {
175191
for (int k = 0; k < 4; ++k) {
176192
const char h = text[++i];
177193
cp <<= 4;
178-
if (h >= '0' && h <= '9') cp |= static_cast<uint32_t>(h - '0');
179-
else if (h >= 'a' && h <= 'f') cp |= static_cast<uint32_t>(h - 'a' + 10);
180-
else if (h >= 'A' && h <= 'F') cp |= static_cast<uint32_t>(h - 'A' + 10);
181-
else throw std::runtime_error("vocab.json: bad hex digit");
194+
if (h >= '0' && h <= '9')
195+
cp |= static_cast<uint32_t>(h - '0');
196+
else if (h >= 'a' && h <= 'f')
197+
cp |= static_cast<uint32_t>(h - 'a' + 10);
198+
else if (h >= 'A' && h <= 'F')
199+
cp |= static_cast<uint32_t>(h - 'A' + 10);
200+
else
201+
throw std::runtime_error("vocab.json: bad hex digit");
182202
}
183203
if (cp >= 0xD800 && cp <= 0xDBFF && i + 6 < n
184204
&& text[i + 1] == '\\' && text[i + 2] == 'u') {
@@ -187,10 +207,14 @@ namespace llm {
187207
for (int k = 0; k < 4; ++k) {
188208
const char h = text[j++];
189209
lo <<= 4;
190-
if (h >= '0' && h <= '9') lo |= static_cast<uint32_t>(h - '0');
191-
else if (h >= 'a' && h <= 'f') lo |= static_cast<uint32_t>(h - 'a' + 10);
192-
else if (h >= 'A' && h <= 'F') lo |= static_cast<uint32_t>(h - 'A' + 10);
193-
else throw std::runtime_error("vocab.json: bad hex digit");
210+
if (h >= '0' && h <= '9')
211+
lo |= static_cast<uint32_t>(h - '0');
212+
else if (h >= 'a' && h <= 'f')
213+
lo |= static_cast<uint32_t>(h - 'a' + 10);
214+
else if (h >= 'A' && h <= 'F')
215+
lo |= static_cast<uint32_t>(h - 'A' + 10);
216+
else
217+
throw std::runtime_error("vocab.json: bad hex digit");
194218
}
195219
if (lo >= 0xDC00 && lo <= 0xDFFF) {
196220
cp = 0x10000u + ((cp - 0xD800u) << 10) + (lo - 0xDC00u);
@@ -212,13 +236,15 @@ namespace llm {
212236
if (i >= n) {
213237
throw std::runtime_error("vocab.json: unterminated string");
214238
}
215-
++i; // consume closing quote
239+
++i; // consume closing quote
216240
};
217241

218242
auto read_int = [&]() {
219243
std::size_t start = i;
220-
if (i < n && (text[i] == '-' || text[i] == '+')) ++i;
221-
while (i < n && text[i] >= '0' && text[i] <= '9') ++i;
244+
if (i < n && (text[i] == '-' || text[i] == '+'))
245+
++i;
246+
while (i < n && text[i] >= '0' && text[i] <= '9')
247+
++i;
222248
return std::stoi(text.substr(start, i - start));
223249
};
224250

@@ -269,18 +295,20 @@ namespace llm {
269295
if (first) {
270296
first = false;
271297
if (line.size() >= 1 && line[0] == '#') {
272-
continue; // skip "#version: 0.2" header
298+
continue; // skip "#version: 0.2" header
273299
}
274300
}
275-
if (line.empty()) continue;
301+
if (line.empty())
302+
continue;
276303
const std::size_t sp = line.find(' ');
277-
if (sp == std::string::npos) continue;
304+
if (sp == std::string::npos)
305+
continue;
278306
merges.emplace_back(line.substr(0, sp), line.substr(sp + 1));
279307
}
280308
return merges;
281309
}
282310

283-
} // namespace internal
311+
} // namespace internal
284312

285313
class gpt2_bpe_tokenizer {
286314
public:
@@ -324,7 +352,7 @@ namespace llm {
324352
std::string concat;
325353
for (int id : ids) {
326354
if (id < 0 || static_cast<std::size_t>(id) >= inv_vocab_.size()) {
327-
continue; // skip unknown
355+
continue; // skip unknown
328356
}
329357
concat += inv_vocab_[static_cast<std::size_t>(id)];
330358
}
@@ -344,7 +372,8 @@ namespace llm {
344372
} else if ((c & 0xF8) == 0xF0) {
345373
len = 4;
346374
}
347-
if (i + len > concat.size()) break;
375+
if (i + len > concat.size())
376+
break;
348377
const std::string ch = concat.substr(i, len);
349378
auto it = unicode_to_byte_.find(ch);
350379
if (it != unicode_to_byte_.end()) {
@@ -390,7 +419,7 @@ namespace llm {
390419
while (i < n) {
391420
// Contractions: 's, 't, 're, 've, 'm, 'll, 'd
392421
if (text[i] == '\'') {
393-
static const char* const contractions[] = {"'s", "'t", "'re", "'ve", "'m", "'ll", "'d"};
422+
static const char* const contractions[] = { "'s", "'t", "'re", "'ve", "'m", "'ll", "'d" };
394423
bool matched = false;
395424
for (const char* c : contractions) {
396425
const std::size_t L = std::strlen(c);
@@ -401,7 +430,8 @@ namespace llm {
401430
break;
402431
}
403432
}
404-
if (matched) continue;
433+
if (matched)
434+
continue;
405435
}
406436

407437
// GPT-2 attaches an optional leading space to letters/digits/
@@ -413,14 +443,16 @@ namespace llm {
413443

414444
if (look < n && is_letter(static_cast<unsigned char>(text[look]))) {
415445
std::size_t j = look;
416-
while (j < n && is_letter(static_cast<unsigned char>(text[j]))) ++j;
446+
while (j < n && is_letter(static_cast<unsigned char>(text[j])))
447+
++j;
417448
out.emplace_back(text.substr(start, j - start));
418449
i = j;
419450
continue;
420451
}
421452
if (look < n && is_digit(static_cast<unsigned char>(text[look]))) {
422453
std::size_t j = look;
423-
while (j < n && is_digit(static_cast<unsigned char>(text[j]))) ++j;
454+
while (j < n && is_digit(static_cast<unsigned char>(text[j])))
455+
++j;
424456
out.emplace_back(text.substr(start, j - start));
425457
i = j;
426458
continue;
@@ -445,7 +477,8 @@ namespace llm {
445477
// skips a character is leading_space).
446478
if (is_space(static_cast<unsigned char>(text[i]))) {
447479
std::size_t j = i;
448-
while (j < n && is_space(static_cast<unsigned char>(text[j]))) ++j;
480+
while (j < n && is_space(static_cast<unsigned char>(text[j])))
481+
++j;
449482
const std::size_t run = j - i;
450483
// Emit the whole run except for a single trailing space
451484
// before non-space content; that space attaches to the
@@ -471,18 +504,24 @@ namespace llm {
471504
// codepoints (each codepoint is one BPE "symbol" initially).
472505
void bpe_encode(const std::string& piece, std::vector<int>& out) const
473506
{
474-
if (piece.empty()) return;
507+
if (piece.empty())
508+
return;
475509
// Split into codepoints.
476510
std::vector<std::string> symbols;
477511
std::size_t i = 0;
478512
while (i < piece.size()) {
479513
const unsigned char c = static_cast<unsigned char>(piece[i]);
480514
std::size_t len = 1;
481-
if ((c & 0x80) == 0) len = 1;
482-
else if ((c & 0xE0) == 0xC0) len = 2;
483-
else if ((c & 0xF0) == 0xE0) len = 3;
484-
else if ((c & 0xF8) == 0xF0) len = 4;
485-
if (i + len > piece.size()) len = piece.size() - i;
515+
if ((c & 0x80) == 0)
516+
len = 1;
517+
else if ((c & 0xE0) == 0xC0)
518+
len = 2;
519+
else if ((c & 0xF0) == 0xE0)
520+
len = 3;
521+
else if ((c & 0xF8) == 0xF0)
522+
len = 4;
523+
if (i + len > piece.size())
524+
len = piece.size() - i;
486525
symbols.emplace_back(piece.substr(i, len));
487526
i += len;
488527
}
@@ -498,7 +537,8 @@ namespace llm {
498537
best_idx = k;
499538
}
500539
}
501-
if (best_idx == symbols.size()) break;
540+
if (best_idx == symbols.size())
541+
break;
502542
symbols[best_idx] += symbols[best_idx + 1];
503543
symbols.erase(symbols.begin() + static_cast<std::ptrdiff_t>(best_idx) + 1);
504544
}
@@ -514,7 +554,8 @@ namespace llm {
514554
const unsigned char ub = static_cast<unsigned char>(ch);
515555
const auto& enc = byte_to_unicode_[ub];
516556
auto it2 = vocab_.find(enc);
517-
if (it2 != vocab_.end()) out.push_back(it2->second);
557+
if (it2 != vocab_.end())
558+
out.push_back(it2->second);
518559
}
519560
}
520561
}
@@ -537,5 +578,5 @@ namespace llm {
537578
std::unordered_map<std::pair<std::string, std::string>, int, pair_hash> merge_ranks_;
538579
};
539580

540-
} // namespace llm
541-
} // namespace fdeep
581+
} // namespace llm
582+
} // namespace fdeep

0 commit comments

Comments
 (0)