Repository: ggerganov/llama.cpp
File: tools/server/server.cpp (C++)

Code before:
                            slot.n_past--;
                            SLT_WRN(slot, "n_past was set to %d\n", slot.n_past);
                        }

                        slot.n_prompt_tokens_cache     = slot.n_past;
                        slot.n_prompt_tokens_processed = 0;
                    }

                    if (!slot.can_split()) {
                        // cannot fit the prompt in the current batch - will try next iter
                        if (batch.n_tokens + slot.n_prompt_tokens > n_batch) {
                            continue;
                        }
                    }

                    // truncate any tokens that are beyond n_past for this slot
                    if (!llama_memory_seq_rm(llama_get_memory(ctx), slot.id, slot.n_past, -1)) {
                        SLT_WRN(slot, "failed to truncate tokens beyond n_past = %d\n", slot.n_past);
                        llama_memory_seq_rm(llama_get_memory(ctx), slot.id, -1, -1);

                        // there is no common part left
                        slot.n_past                = 0;
                        slot.n_prompt_tokens_cache = 0;
                    }

                    SLT_INF(slot, "n_past = %d\n", slot.n_past);

                    // remove the non-common part from the cache
                    slot.cache_tokens.keep_first(slot.n_past);

                    // check if we should process the image
                    if (slot.n_past < slot.n_prompt_tokens && slot.prompt_tokens[slot.n_past] == LLAMA_TOKEN_NULL) {
                        // process the image
                        int32_t new_n_past;
                        int32_t res = slot.prompt_tokens.process_chunk(ctx, mctx, slot.n_past, slot.id, new_n_past);
                        int32_t n_pos = new_n_past - slot.n_past;

                        if (res != 0) {
                            SLT_ERR(slot, "failed to process image, res = %d\n", res);
                            slot.release();
                            send_error(slot, "failed to process image", ERROR_TYPE_SERVER);
                            continue;
                        }

                        // add the image chunk to cache
                        {
                            const auto & chunk = slot.prompt_tokens.find_chunk(slot.n_past);
                            slot.cache_tokens.push_back(chunk.get()); // copy
                        }

                        slot.n_past                    += n_pos;

Review comment (suggestion):
Keep this log so that we remember that the memory has been removed:

```suggestion
                    SLT_INF(slot, "n_past = %d, memory_seq_rm [%d, end)\n", slot.n_past, slot.n_past);
```

Code after:
                            SLT_WRN(slot, "need to evaluate at least 1 token for each active slot (n_past = %d, n_prompt_tokens = %d)\n", slot.n_past, slot.n_prompt_tokens);
                            slot.n_past--;
                            SLT_WRN(slot, "n_past was set to %d\n", slot.n_past);
                        }

                        slot.n_prompt_tokens_cache     = slot.n_past;
                        slot.n_prompt_tokens_processed = 0;
                    }

                    if (!slot.can_split()) {
                        // cannot fit the prompt in the current batch - will try next iter
                        if (batch.n_tokens + slot.n_prompt_tokens > n_batch) {
                            continue;
                        }
                    }

                    // truncate any tokens that are beyond n_past for this slot
                    if (!llama_memory_seq_rm(llama_get_memory(ctx), slot.id, slot.n_past, -1)) {
                        SLT_WRN(slot, "failed to truncate tokens beyond n_past = %d\n", slot.n_past);
                        llama_memory_seq_rm(llama_get_memory(ctx), slot.id, -1, -1);

                        // there is no common part left
                        slot.n_past                = 0;
                        slot.n_prompt_tokens_cache = 0;
                    }

                    SLT_INF(slot, "n_past = %d, memory_seq_rm [%d, end)\n", slot.n_past, slot.n_past);

                    // remove the non-common part from the cache
                    slot.cache_tokens.keep_first(slot.n_past);

                    // check if we should process the image
                    if (slot.n_past < slot.n_prompt_tokens && slot.prompt_tokens[slot.n_past] == LLAMA_TOKEN_NULL) {
                        // process the image
                        int32_t new_n_past;
                        int32_t res = slot.prompt_tokens.process_chunk(ctx, mctx, slot.n_past, slot.id, new_n_past);
                        int32_t n_pos = new_n_past - slot.n_past;

                        if (res != 0) {
                            SLT_ERR(slot, "failed to process image, res = %d\n", res);
                            slot.release();
                            send_error(slot, "failed to process image", ERROR_TYPE_SERVER);
                            continue;
                        }

                        // add the image chunk to cache
                        {
                            const auto & chunk = slot.prompt_tokens.find_chunk(slot.n_past);
                            slot.cache_tokens.push_back(chunk.get()); // copy
                        }