Skip to content

Commit 42eb248

Browse files
authored
common : remove json.hpp from common.cpp (#12697)
* common : remove json.hpp from common.cpp * fix comment
1 parent 9bacd6b commit 42eb248

File tree

4 files changed

+34
-38
lines changed

4 files changed

+34
-38
lines changed

common/common.cpp

-28
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,6 @@
77

88
#include "common.h"
99
#include "log.h"
10-
// Change JSON_ASSERT from assert() to GGML_ASSERT:
11-
#define JSON_ASSERT GGML_ASSERT
12-
#include "json.hpp"
1310
#include "llama.h"
1411

1512
#include <algorithm>
@@ -56,8 +53,6 @@
5653
#pragma warning(disable: 4244 4267) // possible loss of data
5754
#endif
5855

59-
using json = nlohmann::ordered_json;
60-
6156
//
6257
// CPU utils
6358
//
@@ -1545,26 +1540,3 @@ common_control_vector_data common_control_vector_load(const std::vector<common_c
15451540

15461541
return result;
15471542
}
1548-
1549-
template <>
1550-
json common_grammar_trigger::to_json() const {
1551-
json out {
1552-
{"type", (int) type},
1553-
{"value", value},
1554-
};
1555-
if (type == COMMON_GRAMMAR_TRIGGER_TYPE_TOKEN) {
1556-
out["token"] = (int) token;
1557-
}
1558-
return out;
1559-
}
1560-
1561-
template <>
1562-
common_grammar_trigger common_grammar_trigger::from_json(const json & in) {
1563-
common_grammar_trigger out;
1564-
out.type = (common_grammar_trigger_type) in.at("type").get<int>();
1565-
out.value = in.at("value").get<std::string>();
1566-
if (out.type == COMMON_GRAMMAR_TRIGGER_TYPE_TOKEN) {
1567-
out.token = (llama_token) in.at("token").get<int>();
1568-
}
1569-
return out;
1570-
}

common/common.h

-4
Original file line numberDiff line numberDiff line change
@@ -121,10 +121,6 @@ struct common_grammar_trigger {
121121
common_grammar_trigger_type type;
122122
std::string value;
123123
llama_token token = LLAMA_TOKEN_NULL;
124-
125-
// T can only be nlohmann::ordered_json
126-
template <class T> T to_json() const;
127-
template <class T> static common_grammar_trigger from_json(const T & in);
128124
};
129125

130126
// sampling parameters

examples/server/server.cpp

+6-5
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,8 @@ struct slot_params {
133133

134134
auto grammar_triggers = json::array();
135135
for (const auto & trigger : sampling.grammar_triggers) {
136-
grammar_triggers.push_back(trigger.to_json<json>());
136+
server_grammar_trigger ct(std::move(trigger));
137+
grammar_triggers.push_back(ct.to_json());
137138
}
138139

139140
return json {
@@ -372,9 +373,9 @@ struct server_task {
372373
const auto grammar_triggers = data.find("grammar_triggers");
373374
if (grammar_triggers != data.end()) {
374375
for (const auto & t : *grammar_triggers) {
375-
auto ct = common_grammar_trigger::from_json(t);
376-
if (ct.type == COMMON_GRAMMAR_TRIGGER_TYPE_WORD) {
377-
const auto & word = ct.value;
376+
server_grammar_trigger ct(t);
377+
if (ct.value.type == COMMON_GRAMMAR_TRIGGER_TYPE_WORD) {
378+
const auto & word = ct.value.value;
378379
auto ids = common_tokenize(vocab, word, /* add_special= */ false, /* parse_special= */ true);
379380
if (ids.size() == 1) {
380381
auto token = ids[0];
@@ -392,7 +393,7 @@ struct server_task {
392393
params.sampling.grammar_triggers.push_back({COMMON_GRAMMAR_TRIGGER_TYPE_WORD, word});
393394
}
394395
} else {
395-
params.sampling.grammar_triggers.push_back(ct);
396+
params.sampling.grammar_triggers.push_back(std::move(ct.value));
396397
}
397398
}
398399
}

examples/server/utils.hpp

+28-1
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,32 @@ static T json_value(const json & body, const std::string & key, const T & defaul
5858

5959
const static std::string build_info("b" + std::to_string(LLAMA_BUILD_NUMBER) + "-" + LLAMA_COMMIT);
6060

61+
// thin wrapper around common_grammar_trigger with (de)serialization functions
62+
struct server_grammar_trigger {
63+
common_grammar_trigger value;
64+
65+
server_grammar_trigger() = default;
66+
server_grammar_trigger(const common_grammar_trigger & value) : value(value) {}
67+
server_grammar_trigger(const json & in) {
68+
value.type = (common_grammar_trigger_type) in.at("type").get<int>();
69+
value.value = in.at("value").get<std::string>();
70+
if (value.type == COMMON_GRAMMAR_TRIGGER_TYPE_TOKEN) {
71+
value.token = (llama_token) in.at("token").get<int>();
72+
}
73+
}
74+
75+
json to_json() const {
76+
json out {
77+
{"type", (int) value.type},
78+
{"value", value.value},
79+
};
80+
if (value.type == COMMON_GRAMMAR_TRIGGER_TYPE_TOKEN) {
81+
out["token"] = (int) value.token;
82+
}
83+
return out;
84+
}
85+
};
86+
6187
//
6288
// tokenizer and input processing utils
6389
//
@@ -627,7 +653,8 @@ static json oaicompat_completion_params_parse(
627653
llama_params["grammar_lazy"] = chat_params.grammar_lazy;
628654
auto grammar_triggers = json::array();
629655
for (const auto & trigger : chat_params.grammar_triggers) {
630-
grammar_triggers.push_back(trigger.to_json<json>());
656+
server_grammar_trigger ct(trigger);
657+
grammar_triggers.push_back(ct.to_json());
631658
}
632659
llama_params["grammar_triggers"] = grammar_triggers;
633660
llama_params["preserved_tokens"] = chat_params.preserved_tokens;

0 commit comments

Comments
 (0)