llama-memory.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. #pragma once
  2. #include "llama.h"
  3. #include <memory>
  4. struct llama_ubatch;
  5. class llama_batch_allocr;
  6. class llama_io_write_i;
  7. class llama_io_read_i;
  8. struct llama_memory_params {
  9. // kv cache
  10. ggml_type type_k;
  11. ggml_type type_v;
  12. // use full-size SWA cache
  13. bool swa_full;
  14. };
  15. enum llama_memory_status {
  16. LLAMA_MEMORY_STATUS_SUCCESS = 0,
  17. LLAMA_MEMORY_STATUS_NO_UPDATE,
  18. LLAMA_MEMORY_STATUS_FAILED_PREPARE,
  19. LLAMA_MEMORY_STATUS_FAILED_COMPUTE,
  20. };
  21. // helper function for combining the status of two memory contexts
  22. // useful for implementing hybrid memory types (e.g. iSWA)
  23. llama_memory_status llama_memory_status_combine(llama_memory_status s0, llama_memory_status s1);
  24. // helper function for checking if a memory status indicates a failure
  25. bool llama_memory_status_is_fail(llama_memory_status status);
  26. // the interface for managing the memory context during batch processing
  27. // this interface is implemented per memory type. see:
  28. // - llama_kv_cache_unified_context
  29. // - llama_kv_cache_unified_iswa_context
  30. // ...
  31. //
  32. // the only method that should mutate the memory and the memory context is llama_memory_i::apply()
  33. struct llama_memory_context_i {
  34. virtual ~llama_memory_context_i() = default;
  35. // consume the current ubatch from the context and proceed to the next one
  36. // return false if we are done
  37. virtual bool next() = 0;
  38. // apply the memory state for the current ubatch to the memory object
  39. // return false on failure
  40. virtual bool apply() = 0;
  41. // get the current ubatch
  42. virtual const llama_ubatch & get_ubatch() const = 0;
  43. // get the status of the memory context - used for error handling and checking if any updates would be applied
  44. virtual llama_memory_status get_status() const = 0;
  45. };
  46. using llama_memory_context_ptr = std::unique_ptr<llama_memory_context_i>;
  47. // general concept of LLM memory
  48. // the KV cache is a type of LLM memory, but there can be other types
  49. struct llama_memory_i {
  50. virtual ~llama_memory_i() = default;
  51. // split the input batch into a set of ubatches and verify that they can fit into the cache
  52. // return a context object containing the ubatches and memory state required to process them
  53. // check the llama_memory_context_i::get_status() for the result
  54. virtual llama_memory_context_ptr init_batch(
  55. llama_batch_allocr & balloc,
  56. uint32_t n_ubatch,
  57. bool embd_all) = 0;
  58. // simulate full cache, used for allocating worst-case compute buffers
  59. virtual llama_memory_context_ptr init_full() = 0;
  60. // prepare for any pending memory updates, such as shifts, defrags, etc.
  61. // status == LLAMA_MEMORY_STATUS_NO_UPDATE if there is nothing to update
  62. virtual llama_memory_context_ptr init_update(llama_context * lctx, bool optimize) = 0;
  63. // getters
  64. virtual bool get_can_shift() const = 0;
  65. //
  66. // ops
  67. //
  68. // if data == true, the data buffers will also be cleared together with the metadata
  69. virtual void clear(bool data) = 0;
  70. virtual bool seq_rm (llama_seq_id seq_id, llama_pos p0, llama_pos p1) = 0;
  71. virtual void seq_cp (llama_seq_id seq_id_src, llama_seq_id seq_id_dst, llama_pos p0, llama_pos p1) = 0;
  72. virtual void seq_keep(llama_seq_id seq_id) = 0;
  73. virtual void seq_add (llama_seq_id seq_id, llama_pos p0, llama_pos p1, llama_pos shift) = 0;
  74. virtual void seq_div (llama_seq_id seq_id, llama_pos p0, llama_pos p1, int d) = 0;
  75. virtual llama_pos seq_pos_min(llama_seq_id seq_id) const = 0;
  76. virtual llama_pos seq_pos_max(llama_seq_id seq_id) const = 0;
  77. //
  78. // state write/read
  79. //
  80. virtual void state_write(llama_io_write_i & io, llama_seq_id seq_id = -1) const = 0;
  81. virtual void state_read (llama_io_read_i & io, llama_seq_id seq_id = -1) = 0;
  82. };
  83. using llama_memory_ptr = std::unique_ptr<llama_memory_i>;
  84. // TODO: temporary until the llama_kv_cache is removed from the public API
  85. struct llama_kv_cache : public llama_memory_i {
  86. virtual ~llama_kv_cache() = default;
  87. };