Skip to content

Commit dacd957

Browse files
authored
Add .clang-format configuration (openvinotoolkit#312)
1 parent a9ab37e commit dacd957

File tree

3 files changed

+67
-30
lines changed

3 files changed

+67
-30
lines changed

.clang-format

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
BasedOnStyle: Google
2+
IndentWidth: 4
3+
UseTab: Never
4+
ColumnLimit: 120
5+
6+
Language: Cpp
7+
Standard: Cpp11
8+
9+
AccessModifierOffset: -4
10+
AlignConsecutiveMacros: true
11+
AllowAllArgumentsOnNextLine: false
12+
AllowAllConstructorInitializersOnNextLine: false
13+
AllowAllParametersOfDeclarationOnNextLine: false
14+
AllowShortFunctionsOnASingleLine: Empty
15+
AllowShortIfStatementsOnASingleLine: Never
16+
AllowShortLambdasOnASingleLine: Empty
17+
AllowShortLoopsOnASingleLine: false
18+
AlwaysBreakBeforeMultilineStrings: false
19+
BinPackArguments: false
20+
BinPackParameters: false
21+
CommentPragmas: '^#'
22+
DerivePointerAlignment: false
23+
FixNamespaceComments: true
24+
IndentCaseLabels: false
25+
IndentPPDirectives: AfterHash
26+
ForEachMacros:
27+
- foreach
28+
- FOREACH_CHILD

text_generation/causal_lm/cpp/beam_search_causal_lm.cpp

+7-7
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ std::string detokenize(ov::InferRequest& detokenizer, const std::vector<int64_t>
2222
detokenizer.infer();
2323
return detokenizer.get_output_tensor().data<std::string>()[0];
2424
}
25-
}
25+
} // namespace
2626

2727
int main(int argc, char* argv[]) try {
2828
if (argc != 3) {
@@ -32,14 +32,14 @@ int main(int argc, char* argv[]) try {
3232
ov::Core core;
3333
core.add_extension(OPENVINO_TOKENIZERS_PATH); // OPENVINO_TOKENIZERS_PATH is defined in CMakeLists.txt
3434
// tokenizer and detokenizer work on CPU only
35-
ov::InferRequest tokenizer = core.compile_model(
36-
std::string{argv[1]} + "/openvino_tokenizer.xml", "CPU").create_infer_request();
35+
ov::InferRequest tokenizer =
36+
core.compile_model(std::string{argv[1]} + "/openvino_tokenizer.xml", "CPU").create_infer_request();
3737
auto [input_ids, attention_mask] = tokenize(tokenizer, argv[2]);
38-
ov::InferRequest detokenizer = core.compile_model(
39-
std::string{argv[1]} + "/openvino_detokenizer.xml", "CPU").create_infer_request();
38+
ov::InferRequest detokenizer =
39+
core.compile_model(std::string{argv[1]} + "/openvino_detokenizer.xml", "CPU").create_infer_request();
4040
// The model can be compiled for GPU as well
41-
ov::InferRequest lm = core.compile_model(
42-
std::string{argv[1]} + "/openvino_model.xml", "CPU").create_infer_request();
41+
ov::InferRequest lm =
42+
core.compile_model(std::string{argv[1]} + "/openvino_model.xml", "CPU").create_infer_request();
4343
// Initialize inputs
4444
lm.set_tensor("input_ids", input_ids);
4545
lm.set_tensor("attention_mask", attention_mask);

text_generation/causal_lm/cpp/group_beam_searcher.hpp

+32-23
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,10 @@ std::vector<int64_t> kmp_search(const std::vector<int64_t>& haystack, const std:
4444
return res;
4545
}
4646

47-
struct Token {float log_prob; int64_t idx;};
47+
struct Token {
48+
float log_prob;
49+
int64_t idx;
50+
};
4851

4952
std::vector<Token> log_softmax(const ov::Tensor& logits, size_t batch_idx) {
5053
if (logits.get_shape().at(0) <= batch_idx) {
@@ -55,10 +58,10 @@ std::vector<Token> log_softmax(const ov::Tensor& logits, size_t batch_idx) {
5558
size_t sequence_offset = (logits.get_shape().at(1) - 1) * vocab_size;
5659
const float* beam_logits = logits.data<const float>() + batch_offset + sequence_offset;
5760
float max_logit = *std::max_element(beam_logits, beam_logits + vocab_size);
58-
float log_sum = std::log(std::accumulate(
59-
beam_logits, beam_logits + vocab_size, 0.0f, [max_logit](float accumulated, float to_add) {
61+
float log_sum = std::log(
62+
std::accumulate(beam_logits, beam_logits + vocab_size, 0.0f, [max_logit](float accumulated, float to_add) {
6063
return accumulated + std::exp(to_add - max_logit);
61-
}));
64+
}));
6265
std::vector<Token> tokens;
6366
tokens.reserve(vocab_size);
6467
for (size_t idx = 0; idx < vocab_size; ++idx) {
@@ -77,7 +80,7 @@ bool greater(const Beam& left, const Beam& right) {
7780
return left.score > right.score;
7881
}
7982

80-
enum class StopCriteria {early, heuristic, never};
83+
enum class StopCriteria { early, heuristic, never };
8184

8285
struct Parameters {
8386
std::vector<int64_t> prompt;
@@ -90,11 +93,13 @@ struct Parameters {
9093
size_t no_repeat_ngram_size = std::numeric_limits<size_t>::max();
9194
// There's no way to extract special token values from the tokenizer for now
9295
int64_t eos_token = 2;
93-
std::function<bool(const Beam&)> early_finish = [](const Beam&){return false;};
96+
std::function<bool(const Beam&)> early_finish = [](const Beam&) {
97+
return false;
98+
};
9499
};
95100

96101
struct Group {
97-
std::vector<Beam> ongoing; // Best beams in front
102+
std::vector<Beam> ongoing; // Best beams in front
98103
std::vector<Beam> min_heap; // The worst of the best completed beams is the first
99104
bool done = false;
100105

@@ -121,26 +126,30 @@ struct Group {
121126
float best_sum_logprobs = ongoing.front().score;
122127
float worst_score = min_heap.front().score;
123128
switch (parameters.stop_criteria) {
124-
case StopCriteria::early:
125-
done = true;
126-
return;
127-
case StopCriteria::heuristic: {
128-
float highest_attainable_score = best_sum_logprobs / std::pow(float(cur_len), parameters.length_penalty);
129-
done = worst_score >= highest_attainable_score;
130-
return;
131-
}
132-
case StopCriteria::never: {
133-
size_t length = parameters.length_penalty > 0.0 ? parameters.max_new_tokens : cur_len;
134-
float highest_attainable_score = best_sum_logprobs / std::pow(float(length), parameters.length_penalty);
135-
done = worst_score >= highest_attainable_score;
136-
return;
137-
}
138-
default: throw std::runtime_error("Never reached");
129+
case StopCriteria::early:
130+
done = true;
131+
return;
132+
case StopCriteria::heuristic: {
133+
float highest_attainable_score = best_sum_logprobs / std::pow(float(cur_len), parameters.length_penalty);
134+
done = worst_score >= highest_attainable_score;
135+
return;
136+
}
137+
case StopCriteria::never: {
138+
size_t length = parameters.length_penalty > 0.0 ? parameters.max_new_tokens : cur_len;
139+
float highest_attainable_score = best_sum_logprobs / std::pow(float(length), parameters.length_penalty);
140+
done = worst_score >= highest_attainable_score;
141+
return;
142+
}
143+
default:
144+
throw std::runtime_error("Never reached");
139145
}
140146
}
141147
};
142148

143-
struct TokenToBeam {int64_t token_idx; int32_t beam_idx;};
149+
struct TokenToBeam {
150+
int64_t token_idx;
151+
int32_t beam_idx;
152+
};
144153

145154
// GroupBeamSearcher processes logits prduced by a language model and accumulates beams using group beam search
146155
// algorithm. select_next_tokens() returns token ids selected by the algorithm and corresponding beam ids. These values

0 commit comments

Comments
 (0)