llama-memory.h 4.4 KB

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