llama-memory.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. #pragma once
  2. #include "llama.h"
  3. #include <memory>
  4. #include <vector>
  5. struct llama_ubatch;
  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 states
  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. // the interface for managing the memory state during batch processing
  25. // this interface is implemented per memory type. see:
  26. // - llama_kv_cache_unified_state
  27. // - llama_kv_cache_unified_iswa_state
  28. // ...
  29. //
  30. // the only method that can mutate the memory and the memory state is llama_memory_i::apply()
  31. //
  32. // TODO: rename to llama_memory_context_i ?
  33. struct llama_memory_state_i {
  34. virtual ~llama_memory_state_i() = default;
  35. // consume the current ubatch from the state 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. // TODO: this might get reworked in the future when refactoring llama_batch
  42. virtual std::vector<int64_t> & out_ids() = 0;
  43. // get the current ubatch
  44. virtual const llama_ubatch & get_ubatch() const = 0;
  45. // get the status of the memory state - 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_state_ptr = std::unique_ptr<llama_memory_state_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. virtual ~llama_memory_i() = default;
  53. // split the input batch into a set of ubatches and verify that they can fit into the cache
  54. // return a state object containing the ubatches and KV cache state required to process them
  55. // check the llama_memory_state_i::get_status() for the result
  56. virtual llama_memory_state_ptr init_batch(
  57. const llama_batch & batch,
  58. uint32_t n_ubatch,
  59. bool embd_all) = 0;
  60. // simulate full cache, used for allocating worst-case compute buffers
  61. virtual llama_memory_state_ptr init_full() = 0;
  62. // prepare for any pending memory updates, such as shifts, defrags, etc.
  63. // status == LLAMA_MEMORY_STATUS_NO_UPDATE if there is nothing to update
  64. virtual llama_memory_state_ptr init_update(llama_context * lctx, bool optimize) = 0;
  65. // getters
  66. virtual bool get_can_shift() const = 0;
  67. //
  68. // ops
  69. //
  70. // if data == true, the data buffers will also be cleared together with the metadata
  71. virtual void clear(bool data) = 0;
  72. virtual bool seq_rm (llama_seq_id seq_id, llama_pos p0, llama_pos p1) = 0;
  73. virtual void seq_cp (llama_seq_id seq_id_src, llama_seq_id seq_id_dst, llama_pos p0, llama_pos p1) = 0;
  74. virtual void seq_keep(llama_seq_id seq_id) = 0;
  75. virtual void seq_add (llama_seq_id seq_id, llama_pos p0, llama_pos p1, llama_pos shift) = 0;
  76. virtual void seq_div (llama_seq_id seq_id, llama_pos p0, llama_pos p1, int d) = 0;
  77. virtual llama_pos seq_pos_min(llama_seq_id seq_id) const = 0;
  78. virtual llama_pos seq_pos_max(llama_seq_id seq_id) const = 0;
  79. //
  80. // state write/read
  81. //
  82. virtual void state_write(llama_io_write_i & io, llama_seq_id seq_id = -1) const = 0;
  83. virtual void state_read (llama_io_read_i & io, llama_seq_id seq_id = -1) = 0;
  84. };
  85. using llama_memory_ptr = std::unique_ptr<llama_memory_i>;
  86. // TODO: temporary until the llama_kv_cache is removed from the public API
  87. struct llama_kv_cache : public llama_memory_i {
  88. virtual ~llama_kv_cache() = default;
  89. };