vec_rules[i].push_back({LLAMA_GRETYPE_END, 0});
}
+ // Validate that all rule references point to valid rules
+ for (size_t i = 0; i < n_rules; i++) {
+ for (const auto & elem : vec_rules[i]) {
+ if (elem.type == LLAMA_GRETYPE_RULE_REF) {
+ if (elem.value >= n_rules || vec_rules[elem.value].empty()) {
+ LLAMA_LOG_ERROR("invalid grammar: rule %zu references undefined rule %u\n", i, elem.value);
+ return nullptr;
+ }
+ }
+ }
+ }
+
// Check for left recursion
std::vector<bool> rules_visited(n_rules);
std::vector<bool> rules_in_progress(n_rules);
bool res = true;
res = res && state_read_meta(io, strm, cell_count, sinfo, seq_id);
- res = res && state_read_data(io, strm, cell_count, sinfo);
+
+ try {
+ res = res && state_read_data(io, strm, cell_count, sinfo);
+ } catch (...) {
+ res = false;
+ }
if (!res) {
if (seq_id == -1) {
bool res = true;
res = res && state_read_meta(io, cell_count, seq_id);
- res = res && state_read_data(io, cell_count);
+
+ try {
+ res = res && state_read_data(io, cell_count);
+ } catch (...) {
+ res = false;
+ }
if (!res) {
if (seq_id == -1) {
ml.get_key(LLM_KV_ATTENTION_CAUSAL, hparams.causal_attn, false);
ml.get_key(LLM_KV_POOLING_TYPE, hparams.pooling_type, false);
ml.get_key(LLM_KV_BLOCK_COUNT, hparams.n_layer_all);
+ GGML_ASSERT(hparams.n_layer_all > 0 && hparams.n_layer_all <= LLAMA_MAX_LAYERS);
ml.get_key(LLM_KV_EXPERT_COUNT, hparams.n_expert, false);
ml.get_key(LLM_KV_EXPERT_USED_COUNT, hparams.n_expert_used, false);
ml.get_key(LLM_KV_EXPERT_GROUP_COUNT, hparams.n_expert_groups, false);
model->hparams.n_embd_head_k_full = desc->n_embd_head_k;
model->hparams.n_embd_head_v_full = desc->n_embd_head_v;
model->hparams.n_layer_all = desc->n_layer;
+ GGML_ASSERT(desc->n_layer > 0 && desc->n_layer <= LLAMA_MAX_LAYERS);
model->hparams.n_expert = desc->n_expert;
for (uint32_t i = 0; i < desc->n_layer; i++) {
*/
static void llama_sampler_temp_impl(llama_token_data_array * cur_p, float temp) {
+ if (cur_p->size == 0) {
+ return;
+ }
+
if (temp <= 0.0f) {
// find the token with the highest logit and set the rest to -inf
size_t max_i = 0;
token_id = node->value;
token_length = position + 1;
}
+ if (position + 1 >= text.size()) {
+ break;
+ }
node = node->traverse(text[++position]);
}
LLAMA_LOG_INFO("%s: printing all EOG tokens:\n", __func__);
for (auto tid : special_eog_ids) {
+ if (tid < 0 || tid >= (llama_token) id_to_token.size()) {
+ LLAMA_LOG_WARN("%s: EOG token id %d is out of range (vocab size %zu), skipping\n",
+ __func__, tid, id_to_token.size());
+ continue;
+ }
auto & text = id_to_token[tid].text;
LLAMA_LOG_INFO("%s: - %d ('%s')\n", __func__, tid, text.c_str());
llama_token s_id = LLAMA_TOKEN_NULL;
for (auto tid : special_eog_ids) {
+ if (tid < 0 || tid >= (llama_token) id_to_token.size()) {
+ continue;
+ }
const auto & text = id_to_token[tid].text;
if (text == "<|tool_response>") {
has_tool_response = true;
}
std::vector<std::string> llama_vocab::get_bpe_merges() const {
- std::vector<std::string> result(pimpl->bpe_ranks.size());
+ int max_rank = -1;
+ for (const auto & pair : pimpl->bpe_ranks) {
+ max_rank = std::max(max_rank, pair.second);
+ }
+ std::vector<std::string> result(max_rank + 1);
for (const auto & pair : pimpl->bpe_ranks) {
result[pair.second] = pair.first.first + " " + pair.first.second;