@fugood/llama.node 1.4.7 → 1.4.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/binding.ts +8 -0
- package/package.json +15 -15
- package/scripts/llama.cpp.patch +23 -24
- package/src/LlamaContext.cpp +4 -2
- package/src/llama.cpp/common/CMakeLists.txt +2 -0
- package/src/llama.cpp/common/arg.cpp +470 -223
- package/src/llama.cpp/common/arg.h +43 -2
- package/src/llama.cpp/common/chat-peg-parser.cpp +16 -2
- package/src/llama.cpp/common/chat.cpp +140 -0
- package/src/llama.cpp/common/common.cpp +130 -67
- package/src/llama.cpp/common/common.h +44 -17
- package/src/llama.cpp/common/console.cpp +98 -18
- package/src/llama.cpp/common/console.h +30 -8
- package/src/llama.cpp/common/download.cpp +69 -25
- package/src/llama.cpp/common/json-schema-to-grammar.cpp +132 -3
- package/src/llama.cpp/common/json-schema-to-grammar.h +20 -0
- package/src/llama.cpp/common/log.cpp +5 -0
- package/src/llama.cpp/common/log.h +1 -0
- package/src/llama.cpp/common/peg-parser.cpp +1 -1
- package/src/llama.cpp/common/preset.cpp +206 -0
- package/src/llama.cpp/common/preset.h +32 -0
- package/src/llama.cpp/common/sampling.cpp +67 -54
- package/src/llama.cpp/common/sampling.h +8 -0
- package/src/llama.cpp/ggml/CMakeLists.txt +4 -0
- package/src/llama.cpp/ggml/include/ggml-alloc.h +9 -0
- package/src/llama.cpp/ggml/include/ggml-backend.h +1 -0
- package/src/llama.cpp/ggml/include/ggml-cpu.h +1 -0
- package/src/llama.cpp/ggml/include/ggml.h +7 -8
- package/src/llama.cpp/ggml/src/CMakeLists.txt +3 -0
- package/src/llama.cpp/ggml/src/ggml-cpu/CMakeLists.txt +4 -0
- package/src/llama.cpp/ggml/src/ggml-cpu/arch/arm/repack.cpp +285 -0
- package/src/llama.cpp/ggml/src/ggml-cpu/arch-fallback.h +28 -0
- package/src/llama.cpp/ggml/src/ggml-cpu/ggml-cpu.c +111 -45
- package/src/llama.cpp/ggml/src/ggml-cpu/ggml-cpu.cpp +4 -0
- package/src/llama.cpp/ggml/src/ggml-cpu/repack.cpp +288 -1
- package/src/llama.cpp/ggml/src/ggml-cpu/repack.h +8 -0
- package/src/llama.cpp/ggml/src/ggml-cpu/vec.cpp +41 -1
- package/src/llama.cpp/ggml/src/ggml-cpu/vec.h +125 -22
- package/src/llama.cpp/include/llama.h +18 -1
- package/src/llama.cpp/src/llama-arch.cpp +1890 -2248
- package/src/llama.cpp/src/llama-arch.h +9 -2
- package/src/llama.cpp/src/llama-batch.cpp +12 -2
- package/src/llama.cpp/src/llama-batch.h +4 -2
- package/src/llama.cpp/src/llama-context.cpp +93 -23
- package/src/llama.cpp/src/llama-context.h +8 -2
- package/src/llama.cpp/src/llama-graph.cpp +84 -16
- package/src/llama.cpp/src/llama-graph.h +17 -4
- package/src/llama.cpp/src/llama-hparams.cpp +6 -0
- package/src/llama.cpp/src/llama-hparams.h +5 -1
- package/src/llama.cpp/src/llama-impl.cpp +4 -0
- package/src/llama.cpp/src/llama-kv-cache.cpp +90 -42
- package/src/llama.cpp/src/llama-kv-cache.h +19 -2
- package/src/llama.cpp/src/llama-memory-hybrid.cpp +1 -1
- package/src/llama.cpp/src/llama-mmap.cpp +123 -28
- package/src/llama.cpp/src/llama-mmap.h +5 -1
- package/src/llama.cpp/src/llama-model-loader.cpp +58 -13
- package/src/llama.cpp/src/llama-model-loader.h +2 -0
- package/src/llama.cpp/src/llama-model.cpp +110 -49
- package/src/llama.cpp/src/llama-model.h +1 -0
- package/src/llama.cpp/src/llama-quant.cpp +1 -1
- package/src/llama.cpp/src/llama-sampling.cpp +16 -0
- package/src/llama.cpp/src/llama-vocab.cpp +2 -1
- package/src/llama.cpp/src/llama.cpp +665 -1
- package/src/llama.cpp/src/models/deepseek2.cpp +9 -5
- package/src/llama.cpp/src/models/glm4-moe.cpp +28 -11
- package/src/llama.cpp/src/models/glm4.cpp +27 -4
- package/src/llama.cpp/src/models/models.h +5 -5
- package/src/llama.cpp/src/models/nemotron-h.cpp +35 -6
- package/src/llama.cpp/src/models/qwen2.cpp +12 -3
- package/src/llama.cpp/src/models/qwen3next.cpp +81 -266
|
@@ -34,6 +34,7 @@ struct llama_hparams_convnext {
|
|
|
34
34
|
|
|
35
35
|
struct llama_hparams {
|
|
36
36
|
bool vocab_only;
|
|
37
|
+
bool no_alloc;
|
|
37
38
|
bool rope_finetuned;
|
|
38
39
|
bool use_par_res;
|
|
39
40
|
bool swin_norm;
|
|
@@ -107,6 +108,7 @@ struct llama_hparams {
|
|
|
107
108
|
float rope_freq_base_train_swa;
|
|
108
109
|
float rope_freq_scale_train;
|
|
109
110
|
float rope_freq_scale_train_swa;
|
|
111
|
+
|
|
110
112
|
uint32_t n_ctx_orig_yarn;
|
|
111
113
|
float rope_yarn_log_mul = 0.0f;
|
|
112
114
|
|
|
@@ -164,6 +166,7 @@ struct llama_hparams {
|
|
|
164
166
|
uint32_t n_no_rope_layer_step = 4;
|
|
165
167
|
uint32_t n_attn_temp_floor_scale = 0;
|
|
166
168
|
float f_attn_temp_scale = 0.0f;
|
|
169
|
+
float f_attn_temp_offset = 0.0f; // offset position index
|
|
167
170
|
|
|
168
171
|
// gemma3n altup
|
|
169
172
|
uint32_t n_altup = 4; // altup_num_inputs
|
|
@@ -267,7 +270,8 @@ struct llama_hparams {
|
|
|
267
270
|
// TODO: think of a better place for this function
|
|
268
271
|
// TODO: pack the SWA params in a struct?
|
|
269
272
|
static bool is_masked_swa(uint32_t n_swa, llama_swa_type swa_type, llama_pos p0, llama_pos p1);
|
|
273
|
+
|
|
274
|
+
bool use_mrope() const;
|
|
270
275
|
};
|
|
271
276
|
|
|
272
277
|
static_assert(std::is_trivially_copyable<llama_hparams>::value, "llama_hparams must be trivially copyable");
|
|
273
|
-
|
|
@@ -25,6 +25,10 @@ time_meas::~time_meas() {
|
|
|
25
25
|
}
|
|
26
26
|
}
|
|
27
27
|
|
|
28
|
+
void llama_log_get(ggml_log_callback * log_callback, void ** user_data) {
|
|
29
|
+
ggml_log_get(log_callback, user_data);
|
|
30
|
+
}
|
|
31
|
+
|
|
28
32
|
void llama_log_set(ggml_log_callback log_callback, void * user_data) {
|
|
29
33
|
ggml_log_set(log_callback, user_data);
|
|
30
34
|
g_logger_state.log_callback = log_callback ? log_callback : llama_log_callback_default;
|
|
@@ -175,7 +175,15 @@ llama_kv_cache::llama_kv_cache(
|
|
|
175
175
|
|
|
176
176
|
// allocate tensors and initialize the buffers to avoid NaNs in the padding
|
|
177
177
|
for (auto & [buft, ctx] : ctx_map) {
|
|
178
|
-
ggml_backend_buffer_t buf
|
|
178
|
+
ggml_backend_buffer_t buf;
|
|
179
|
+
if (model.hparams.no_alloc) {
|
|
180
|
+
buf = ggml_backend_buft_alloc_buffer(buft, /*size =*/ 0); // dummy buffer
|
|
181
|
+
for (ggml_tensor * t = ggml_get_first_tensor(ctx.get()); t != nullptr; t = ggml_get_next_tensor(ctx.get(), t)) {
|
|
182
|
+
t->buffer = buf; // set dummy buffer for KV cache so that the backend scheduler won't try to allocate it
|
|
183
|
+
}
|
|
184
|
+
} else {
|
|
185
|
+
buf = ggml_backend_alloc_ctx_tensors_from_buft(ctx.get(), buft); // real buffer
|
|
186
|
+
}
|
|
179
187
|
if (!buf) {
|
|
180
188
|
throw std::runtime_error("failed to allocate buffer for kv cache");
|
|
181
189
|
}
|
|
@@ -482,9 +490,18 @@ llama_pos llama_kv_cache::seq_pos_max(llama_seq_id seq_id) const {
|
|
|
482
490
|
|
|
483
491
|
std::map<ggml_backend_buffer_type_t, size_t> llama_kv_cache::memory_breakdown() const {
|
|
484
492
|
std::map<ggml_backend_buffer_type_t, size_t> ret;
|
|
485
|
-
for (const auto & [
|
|
486
|
-
|
|
493
|
+
for (const auto & [ctx, buf] : ctxs_bufs) {
|
|
494
|
+
ggml_backend_buffer_type_t buft = ggml_backend_buffer_get_type(buf.get());
|
|
495
|
+
|
|
496
|
+
if (hparams.no_alloc) {
|
|
497
|
+
GGML_ASSERT(ggml_backend_buffer_get_base(buf.get()) == nullptr);
|
|
498
|
+
ret[buft] += ggml_backend_alloc_ctx_tensors_from_buft_size(ctx.get(), buft);
|
|
499
|
+
} else {
|
|
500
|
+
// GGML_ASSERT(ggml_backend_buffer_get_base(buf.get()) != nullptr); // multi_buffer does not have a defined base
|
|
501
|
+
ret[buft] += ggml_backend_buffer_get_size(buf.get());
|
|
502
|
+
}
|
|
487
503
|
}
|
|
504
|
+
|
|
488
505
|
return ret;
|
|
489
506
|
}
|
|
490
507
|
|
|
@@ -1232,8 +1249,7 @@ void llama_kv_cache::set_input_kq_mask(ggml_tensor * dst, const llama_ubatch * u
|
|
|
1232
1249
|
GGML_ASSERT(n_tokens%n_stream == 0);
|
|
1233
1250
|
|
|
1234
1251
|
// n_tps == n_tokens_per_stream
|
|
1235
|
-
const int64_t n_tps
|
|
1236
|
-
const int64_t n_tps_pad = GGML_PAD(n_tps, GGML_KQ_MASK_PAD);
|
|
1252
|
+
const int64_t n_tps = n_tokens/n_stream;
|
|
1237
1253
|
|
|
1238
1254
|
std::fill(data, data + ggml_nelements(dst), -INFINITY);
|
|
1239
1255
|
|
|
@@ -1266,7 +1282,7 @@ void llama_kv_cache::set_input_kq_mask(ggml_tensor * dst, const llama_ubatch * u
|
|
|
1266
1282
|
const llama_pos p1_x = is_2d ? ubatch->pos[i + ubatch->n_tokens*2] : 0;
|
|
1267
1283
|
const llama_pos p1_y = is_2d ? ubatch->pos[i + ubatch->n_tokens] : 0;
|
|
1268
1284
|
|
|
1269
|
-
const uint64_t idst = n_kv*(h*n_stream*
|
|
1285
|
+
const uint64_t idst = n_kv*(h*n_stream*n_tps + s*n_tps + ii);
|
|
1270
1286
|
|
|
1271
1287
|
for (uint32_t j = 0; j < n_kv; ++j) {
|
|
1272
1288
|
if (cells.is_empty(j)) {
|
|
@@ -1370,9 +1386,10 @@ ggml_tensor * llama_kv_cache::build_rope_shift(
|
|
|
1370
1386
|
float freq_scale) const {
|
|
1371
1387
|
const auto & n_ctx_orig = cparams.n_ctx_orig_yarn;
|
|
1372
1388
|
|
|
1373
|
-
const auto & yarn_ext_factor
|
|
1374
|
-
const auto & yarn_beta_fast
|
|
1375
|
-
const auto & yarn_beta_slow
|
|
1389
|
+
const auto & yarn_ext_factor = cparams.yarn_ext_factor;
|
|
1390
|
+
const auto & yarn_beta_fast = cparams.yarn_beta_fast;
|
|
1391
|
+
const auto & yarn_beta_slow = cparams.yarn_beta_slow;
|
|
1392
|
+
const auto & yarn_attn_factor = cparams.yarn_attn_factor;
|
|
1376
1393
|
|
|
1377
1394
|
const auto & n_rot = hparams.n_rot;
|
|
1378
1395
|
const auto & rope_type = hparams.rope_type == LLAMA_ROPE_TYPE_MROPE || hparams.rope_type == LLAMA_ROPE_TYPE_IMROPE
|
|
@@ -1383,12 +1400,6 @@ ggml_tensor * llama_kv_cache::build_rope_shift(
|
|
|
1383
1400
|
? LLAMA_ROPE_TYPE_NEOX
|
|
1384
1401
|
: hparams.rope_type;
|
|
1385
1402
|
|
|
1386
|
-
// See llm_build_deepseek2() for why attn_factor has to be scaled for YaRN RoPE to work correctly.
|
|
1387
|
-
// See https://github.com/ggerganov/llama.cpp/discussions/7416 for detailed explanation.
|
|
1388
|
-
const float yarn_attn_factor = model.arch == LLM_ARCH_DEEPSEEK2
|
|
1389
|
-
? 1.0f / (1.0f + 0.1f * logf(1.0f / freq_scale))
|
|
1390
|
-
: cparams.yarn_attn_factor;
|
|
1391
|
-
|
|
1392
1403
|
ggml_tensor * tmp;
|
|
1393
1404
|
|
|
1394
1405
|
if (ggml_is_quantized(cur->type)) {
|
|
@@ -1550,9 +1561,11 @@ void llama_kv_cache::state_read(llama_io_read_i & io, llama_seq_id seq_id, llama
|
|
|
1550
1561
|
|
|
1551
1562
|
const uint32_t strm = seq_id == -1 ? s : seq_to_stream[seq_id];
|
|
1552
1563
|
|
|
1564
|
+
slot_info sinfo;
|
|
1565
|
+
|
|
1553
1566
|
bool res = true;
|
|
1554
|
-
res = res && state_read_meta(io, strm, cell_count, seq_id);
|
|
1555
|
-
res = res && state_read_data(io, strm, cell_count);
|
|
1567
|
+
res = res && state_read_meta(io, strm, cell_count, sinfo, seq_id);
|
|
1568
|
+
res = res && state_read_data(io, strm, cell_count, sinfo);
|
|
1556
1569
|
|
|
1557
1570
|
if (!res) {
|
|
1558
1571
|
if (seq_id == -1) {
|
|
@@ -1691,7 +1704,7 @@ void llama_kv_cache::state_write_data(llama_io_write_i & io, const cell_ranges_t
|
|
|
1691
1704
|
}
|
|
1692
1705
|
}
|
|
1693
1706
|
|
|
1694
|
-
bool llama_kv_cache::state_read_meta(llama_io_read_i & io, uint32_t strm, uint32_t cell_count, llama_seq_id dest_seq_id) {
|
|
1707
|
+
bool llama_kv_cache::state_read_meta(llama_io_read_i & io, uint32_t strm, uint32_t cell_count, slot_info & sinfo, llama_seq_id dest_seq_id) {
|
|
1695
1708
|
auto & cells = v_cells[strm];
|
|
1696
1709
|
auto & head = v_heads[strm];
|
|
1697
1710
|
|
|
@@ -1728,7 +1741,7 @@ bool llama_kv_cache::state_read_meta(llama_io_read_i & io, uint32_t strm, uint32
|
|
|
1728
1741
|
ubatch.seq_id[i] = &dest_seq_id;
|
|
1729
1742
|
}
|
|
1730
1743
|
|
|
1731
|
-
|
|
1744
|
+
sinfo = find_slot(ubatch, false);
|
|
1732
1745
|
if (sinfo.empty()) {
|
|
1733
1746
|
LLAMA_LOG_ERROR("%s: failed to find available cells in kv cache\n", __func__);
|
|
1734
1747
|
return false;
|
|
@@ -1738,20 +1751,16 @@ bool llama_kv_cache::state_read_meta(llama_io_read_i & io, uint32_t strm, uint32
|
|
|
1738
1751
|
// see: https://github.com/ggml-org/llama.cpp/pull/16825#issuecomment-3460868350
|
|
1739
1752
|
apply_ubatch(sinfo, ubatch);
|
|
1740
1753
|
|
|
1741
|
-
|
|
1742
|
-
|
|
1743
|
-
// keep the head at the old position because we will read the KV data into it in state_read_data()
|
|
1744
|
-
head = head_cur;
|
|
1754
|
+
LLAMA_LOG_DEBUG("%s: cell_count = %d, dest_seq_id = %d\n", __func__, cell_count, dest_seq_id);
|
|
1745
1755
|
|
|
1746
|
-
|
|
1747
|
-
|
|
1748
|
-
|
|
1749
|
-
|
|
1750
|
-
|
|
1751
|
-
|
|
1752
|
-
|
|
1753
|
-
|
|
1754
|
-
GGML_ASSERT(cells.seq_has(head_cur + cell_count - 1, dest_seq_id));
|
|
1756
|
+
// DEBUG CHECK: verify that all cells were allocated and have correct seq_id and pos values
|
|
1757
|
+
GGML_ASSERT(sinfo.n_stream() == 1);
|
|
1758
|
+
GGML_ASSERT(sinfo.idxs[0].size() == cell_count);
|
|
1759
|
+
for (uint32_t i = 0; i < cell_count; ++i) {
|
|
1760
|
+
const uint32_t idx = sinfo.idxs[0][i];
|
|
1761
|
+
GGML_ASSERT(cells.pos_get(idx) == ubatch.pos[i]);
|
|
1762
|
+
GGML_ASSERT(cells.seq_has(idx, dest_seq_id));
|
|
1763
|
+
}
|
|
1755
1764
|
} else {
|
|
1756
1765
|
// whole KV cache restore
|
|
1757
1766
|
|
|
@@ -1784,15 +1793,24 @@ bool llama_kv_cache::state_read_meta(llama_io_read_i & io, uint32_t strm, uint32
|
|
|
1784
1793
|
}
|
|
1785
1794
|
}
|
|
1786
1795
|
|
|
1796
|
+
// Create contiguous slot_info for whole cache restore
|
|
1797
|
+
sinfo.s0 = strm;
|
|
1798
|
+
sinfo.s1 = strm;
|
|
1799
|
+
sinfo.resize(1);
|
|
1800
|
+
sinfo.strm[0] = strm;
|
|
1801
|
+
sinfo.idxs[0].resize(cell_count);
|
|
1802
|
+
for (uint32_t i = 0; i < cell_count; ++i) {
|
|
1803
|
+
sinfo.idxs[0][i] = i;
|
|
1804
|
+
}
|
|
1805
|
+
|
|
1787
1806
|
head = 0;
|
|
1788
1807
|
}
|
|
1789
1808
|
|
|
1790
1809
|
return true;
|
|
1791
1810
|
}
|
|
1792
1811
|
|
|
1793
|
-
bool llama_kv_cache::state_read_data(llama_io_read_i & io, uint32_t strm, uint32_t cell_count) {
|
|
1812
|
+
bool llama_kv_cache::state_read_data(llama_io_read_i & io, uint32_t strm, uint32_t cell_count, const slot_info & sinfo) {
|
|
1794
1813
|
auto & cells = v_cells[strm];
|
|
1795
|
-
auto & head = v_heads[strm];
|
|
1796
1814
|
|
|
1797
1815
|
uint32_t v_trans;
|
|
1798
1816
|
uint32_t n_layer;
|
|
@@ -1842,8 +1860,17 @@ bool llama_kv_cache::state_read_data(llama_io_read_i & io, uint32_t strm, uint32
|
|
|
1842
1860
|
}
|
|
1843
1861
|
|
|
1844
1862
|
if (cell_count) {
|
|
1845
|
-
|
|
1846
|
-
|
|
1863
|
+
if (sinfo.is_contiguous()) {
|
|
1864
|
+
// Fast path: contiguous cells, single memcpy
|
|
1865
|
+
ggml_backend_tensor_set(k, io.read(cell_count * k_size_row), sinfo.head() * k_size_row, cell_count * k_size_row);
|
|
1866
|
+
} else {
|
|
1867
|
+
// Slow path: scatter to non-contiguous positions
|
|
1868
|
+
const void * src = io.read(cell_count * k_size_row);
|
|
1869
|
+
for (uint32_t i = 0; i < cell_count; ++i) {
|
|
1870
|
+
const size_t dst_offset = sinfo.idxs[0][i] * k_size_row;
|
|
1871
|
+
ggml_backend_tensor_set(k, (const char*)src + i * k_size_row, dst_offset, k_size_row);
|
|
1872
|
+
}
|
|
1873
|
+
}
|
|
1847
1874
|
}
|
|
1848
1875
|
}
|
|
1849
1876
|
|
|
@@ -1874,8 +1901,17 @@ bool llama_kv_cache::state_read_data(llama_io_read_i & io, uint32_t strm, uint32
|
|
|
1874
1901
|
}
|
|
1875
1902
|
|
|
1876
1903
|
if (cell_count) {
|
|
1877
|
-
|
|
1878
|
-
|
|
1904
|
+
if (sinfo.is_contiguous()) {
|
|
1905
|
+
// Fast path: contiguous cells, single memcpy
|
|
1906
|
+
ggml_backend_tensor_set(v, io.read(cell_count * v_size_row), sinfo.head() * v_size_row, cell_count * v_size_row);
|
|
1907
|
+
} else {
|
|
1908
|
+
// Slow path: scatter to non-contiguous positions
|
|
1909
|
+
const void * src = io.read(cell_count * v_size_row);
|
|
1910
|
+
for (uint32_t i = 0; i < cell_count; ++i) {
|
|
1911
|
+
const size_t dst_offset = sinfo.idxs[0][i] * v_size_row;
|
|
1912
|
+
ggml_backend_tensor_set(v, (const char*)src + i * v_size_row, dst_offset, v_size_row);
|
|
1913
|
+
}
|
|
1914
|
+
}
|
|
1879
1915
|
}
|
|
1880
1916
|
}
|
|
1881
1917
|
} else {
|
|
@@ -1914,10 +1950,22 @@ bool llama_kv_cache::state_read_data(llama_io_read_i & io, uint32_t strm, uint32
|
|
|
1914
1950
|
}
|
|
1915
1951
|
|
|
1916
1952
|
if (cell_count) {
|
|
1917
|
-
|
|
1918
|
-
|
|
1919
|
-
const
|
|
1920
|
-
|
|
1953
|
+
if (sinfo.is_contiguous()) {
|
|
1954
|
+
// Fast path: contiguous cells
|
|
1955
|
+
const uint32_t h = sinfo.head();
|
|
1956
|
+
for (uint32_t j = 0; j < n_embd_v_gqa; ++j) {
|
|
1957
|
+
const size_t dst_offset = (h + j * cells.size()) * v_size_el;
|
|
1958
|
+
ggml_backend_tensor_set(v, io.read(cell_count * v_size_el), dst_offset, cell_count * v_size_el);
|
|
1959
|
+
}
|
|
1960
|
+
} else {
|
|
1961
|
+
// Slow path: scatter to non-contiguous positions
|
|
1962
|
+
for (uint32_t j = 0; j < n_embd_v_gqa; ++j) {
|
|
1963
|
+
const void * src = io.read(cell_count * v_size_el);
|
|
1964
|
+
for (uint32_t i = 0; i < cell_count; ++i) {
|
|
1965
|
+
const size_t dst_offset = (sinfo.idxs[0][i] + j * cells.size()) * v_size_el;
|
|
1966
|
+
ggml_backend_tensor_set(v, (const char*)src + i * v_size_el, dst_offset, v_size_el);
|
|
1967
|
+
}
|
|
1968
|
+
}
|
|
1921
1969
|
}
|
|
1922
1970
|
}
|
|
1923
1971
|
}
|
|
@@ -72,6 +72,23 @@ public:
|
|
|
72
72
|
void clear() {
|
|
73
73
|
idxs.clear();
|
|
74
74
|
}
|
|
75
|
+
|
|
76
|
+
// check if indices are contiguous starting from head()
|
|
77
|
+
bool is_contiguous() const {
|
|
78
|
+
if (idxs.empty() || idxs[0].empty()) {
|
|
79
|
+
return true;
|
|
80
|
+
}
|
|
81
|
+
if (idxs.size() > 1) {
|
|
82
|
+
return false;
|
|
83
|
+
}
|
|
84
|
+
const uint32_t h = idxs[0][0];
|
|
85
|
+
for (size_t i = 0; i < idxs[0].size(); ++i) {
|
|
86
|
+
if (idxs[0][i] != h + i) {
|
|
87
|
+
return false;
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
return true;
|
|
91
|
+
}
|
|
75
92
|
};
|
|
76
93
|
|
|
77
94
|
using slot_info_vec_t = std::vector<slot_info>;
|
|
@@ -264,8 +281,8 @@ private:
|
|
|
264
281
|
void state_write_meta(llama_io_write_i & io, const cell_ranges_t & cr, llama_seq_id seq_id = -1) const;
|
|
265
282
|
void state_write_data(llama_io_write_i & io, const cell_ranges_t & cr) const;
|
|
266
283
|
|
|
267
|
-
bool state_read_meta(llama_io_read_i & io, uint32_t strm, uint32_t cell_count, llama_seq_id dest_seq_id = -1);
|
|
268
|
-
bool state_read_data(llama_io_read_i & io, uint32_t strm, uint32_t cell_count);
|
|
284
|
+
bool state_read_meta(llama_io_read_i & io, uint32_t strm, uint32_t cell_count, slot_info & sinfo, llama_seq_id dest_seq_id = -1);
|
|
285
|
+
bool state_read_data(llama_io_read_i & io, uint32_t strm, uint32_t cell_count, const slot_info & sinfo);
|
|
269
286
|
};
|
|
270
287
|
|
|
271
288
|
class llama_kv_cache_context : public llama_memory_context_i {
|
|
@@ -222,7 +222,7 @@ llama_memory_hybrid_context::llama_memory_hybrid_context(
|
|
|
222
222
|
ubatches(std::move(ubatches)),
|
|
223
223
|
// note: here we copy the ubatches. not sure if this is ideal
|
|
224
224
|
ctx_attn(new llama_kv_cache_context(mem->get_mem_attn(), std::move(sinfos_attn), this->ubatches)),
|
|
225
|
-
ctx_recr(new llama_memory_recurrent_context(mem->get_mem_recr(),
|
|
225
|
+
ctx_recr(new llama_memory_recurrent_context(mem->get_mem_recr(), this->ubatches)),
|
|
226
226
|
status(llama_memory_status_combine(ctx_attn->get_status(), ctx_recr->get_status())) {
|
|
227
227
|
}
|
|
228
228
|
|
|
@@ -13,9 +13,10 @@
|
|
|
13
13
|
#ifdef __has_include
|
|
14
14
|
#if __has_include(<unistd.h>)
|
|
15
15
|
#include <unistd.h>
|
|
16
|
+
#include <fcntl.h>
|
|
17
|
+
#include <sys/stat.h>
|
|
16
18
|
#if defined(_POSIX_MAPPED_FILES)
|
|
17
19
|
#include <sys/mman.h>
|
|
18
|
-
#include <fcntl.h>
|
|
19
20
|
#endif
|
|
20
21
|
#if defined(_POSIX_MEMLOCK_RANGE)
|
|
21
22
|
#include <sys/resource.h>
|
|
@@ -74,7 +75,7 @@ struct llama_file::impl {
|
|
|
74
75
|
return ret;
|
|
75
76
|
}
|
|
76
77
|
|
|
77
|
-
impl(const char * fname, const char * mode) {
|
|
78
|
+
impl(const char * fname, const char * mode, [[maybe_unused]] const bool use_direct_io = false) {
|
|
78
79
|
fp = ggml_fopen(fname, mode);
|
|
79
80
|
if (fp == NULL) {
|
|
80
81
|
throw std::runtime_error(format("failed to open %s: %s", fname, strerror(errno)));
|
|
@@ -153,13 +154,40 @@ struct llama_file::impl {
|
|
|
153
154
|
write_raw(&val, sizeof(val));
|
|
154
155
|
}
|
|
155
156
|
|
|
157
|
+
void read_aligned_chunk(size_t offset, void * dest, size_t size) const {
|
|
158
|
+
throw std::runtime_error("DirectIO is not implemented on Windows.");
|
|
159
|
+
}
|
|
160
|
+
|
|
156
161
|
~impl() {
|
|
157
162
|
if (fp) {
|
|
158
163
|
std::fclose(fp);
|
|
159
164
|
}
|
|
160
165
|
}
|
|
161
166
|
#else
|
|
162
|
-
impl(const char * fname, const char * mode) {
|
|
167
|
+
impl(const char * fname, const char * mode, [[maybe_unused]] const bool use_direct_io = false) {
|
|
168
|
+
#ifdef __linux__
|
|
169
|
+
// Try unbuffered I/O for read only
|
|
170
|
+
if (use_direct_io && std::strcmp(mode, "rb") == 0) {
|
|
171
|
+
fd = open(fname, O_RDONLY | O_DIRECT);
|
|
172
|
+
|
|
173
|
+
if (fd != -1) {
|
|
174
|
+
struct stat file_stats{};
|
|
175
|
+
fstat(fd, &file_stats);
|
|
176
|
+
|
|
177
|
+
size = file_stats.st_size;
|
|
178
|
+
alignment = file_stats.st_blksize;
|
|
179
|
+
|
|
180
|
+
off_t ret = lseek(fd, 0, SEEK_SET);
|
|
181
|
+
if (ret == -1) {
|
|
182
|
+
throw std::runtime_error(format("seek error: %s", strerror(errno)));
|
|
183
|
+
}
|
|
184
|
+
return;
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
LLAMA_LOG_WARN("Failed to open model %s with error: %s. Falling back to buffered I/O",
|
|
188
|
+
fname, strerror(errno));
|
|
189
|
+
}
|
|
190
|
+
#endif
|
|
163
191
|
fp = ggml_fopen(fname, mode);
|
|
164
192
|
if (fp == NULL) {
|
|
165
193
|
throw std::runtime_error(format("failed to open %s: %s", fname, strerror(errno)));
|
|
@@ -170,27 +198,30 @@ struct llama_file::impl {
|
|
|
170
198
|
}
|
|
171
199
|
|
|
172
200
|
size_t tell() const {
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
throw std::runtime_error(format("ftell error: %s", strerror(errno)));
|
|
201
|
+
if (fd == -1) {
|
|
202
|
+
long ret = std::ftell(fp);
|
|
203
|
+
if (ret == -1) {
|
|
204
|
+
throw std::runtime_error(format("ftell error: %s", strerror(errno)));
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
return (size_t) ret;
|
|
181
208
|
}
|
|
182
209
|
|
|
183
|
-
|
|
210
|
+
off_t pos = lseek(fd, 0, SEEK_CUR);
|
|
211
|
+
if (pos == -1) {
|
|
212
|
+
throw std::runtime_error(format("lseek error: %s", strerror(errno)));
|
|
213
|
+
}
|
|
214
|
+
return (size_t) pos;
|
|
184
215
|
}
|
|
185
216
|
|
|
186
217
|
void seek(size_t offset, int whence) const {
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
if (ret
|
|
218
|
+
off_t ret = 0;
|
|
219
|
+
if (fd == -1) {
|
|
220
|
+
ret = std::fseek(fp, (long) offset, whence);
|
|
221
|
+
} else {
|
|
222
|
+
ret = lseek(fd, offset, whence);
|
|
223
|
+
}
|
|
224
|
+
if (ret == -1) {
|
|
194
225
|
throw std::runtime_error(format("seek error: %s", strerror(errno)));
|
|
195
226
|
}
|
|
196
227
|
}
|
|
@@ -200,13 +231,55 @@ struct llama_file::impl {
|
|
|
200
231
|
return;
|
|
201
232
|
}
|
|
202
233
|
errno = 0;
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
234
|
+
if (fd == -1) {
|
|
235
|
+
std::size_t ret = std::fread(ptr, len, 1, fp);
|
|
236
|
+
if (ferror(fp)) {
|
|
237
|
+
throw std::runtime_error(format("read error: %s", strerror(errno)));
|
|
238
|
+
}
|
|
239
|
+
if (ret != 1) {
|
|
240
|
+
throw std::runtime_error("unexpectedly reached end of file");
|
|
241
|
+
}
|
|
242
|
+
} else {
|
|
243
|
+
bool successful = false;
|
|
244
|
+
while (!successful) {
|
|
245
|
+
off_t ret = read(fd, ptr, len);
|
|
246
|
+
|
|
247
|
+
if (ret == -1) {
|
|
248
|
+
if (errno == EINTR) {
|
|
249
|
+
continue; // Interrupted by signal, retry
|
|
250
|
+
}
|
|
251
|
+
throw std::runtime_error(format("read error: %s", strerror(errno)));
|
|
252
|
+
}
|
|
253
|
+
if (ret == 0) {
|
|
254
|
+
throw std::runtime_error("unexpectedly reached end of file");
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
successful = true;
|
|
258
|
+
}
|
|
206
259
|
}
|
|
207
|
-
|
|
208
|
-
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
void read_aligned_chunk(size_t offset, void * dest, size_t size) const {
|
|
263
|
+
off_t aligned_offset = offset & ~(alignment - 1);
|
|
264
|
+
off_t offset_from_alignment = offset - aligned_offset;
|
|
265
|
+
size_t bytes_to_read = (offset_from_alignment + size + alignment - 1) & ~(alignment - 1);
|
|
266
|
+
|
|
267
|
+
void * raw_buffer = nullptr;
|
|
268
|
+
int ret = posix_memalign(&raw_buffer, alignment, bytes_to_read);
|
|
269
|
+
if (ret != 0) {
|
|
270
|
+
throw std::runtime_error(format("posix_memalign failed with error %d", ret));
|
|
209
271
|
}
|
|
272
|
+
|
|
273
|
+
struct aligned_buffer_deleter {
|
|
274
|
+
void operator()(void * p) const { free(p); }
|
|
275
|
+
};
|
|
276
|
+
std::unique_ptr<void, aligned_buffer_deleter> buffer(raw_buffer);
|
|
277
|
+
|
|
278
|
+
seek(aligned_offset, SEEK_SET);
|
|
279
|
+
read_raw(buffer.get(), bytes_to_read);
|
|
280
|
+
|
|
281
|
+
uintptr_t actual_data = reinterpret_cast<uintptr_t>(buffer.get()) + offset_from_alignment;
|
|
282
|
+
memcpy(dest, reinterpret_cast<void *>(actual_data), size);
|
|
210
283
|
}
|
|
211
284
|
|
|
212
285
|
uint32_t read_u32() const {
|
|
@@ -231,22 +304,43 @@ struct llama_file::impl {
|
|
|
231
304
|
}
|
|
232
305
|
|
|
233
306
|
~impl() {
|
|
234
|
-
if (
|
|
307
|
+
if (fd != -1) {
|
|
308
|
+
close(fd);
|
|
309
|
+
} else {
|
|
235
310
|
std::fclose(fp);
|
|
236
311
|
}
|
|
237
312
|
}
|
|
313
|
+
int fd = -1;
|
|
238
314
|
#endif
|
|
239
315
|
|
|
240
|
-
|
|
241
|
-
|
|
316
|
+
void read_raw_at(void * ptr, size_t len, size_t offset) const {
|
|
317
|
+
if (alignment != 1) {
|
|
318
|
+
read_aligned_chunk(offset, ptr, len);
|
|
319
|
+
} else {
|
|
320
|
+
seek(offset, SEEK_SET);
|
|
321
|
+
read_raw(ptr, len);
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
size_t read_alignment() const {
|
|
326
|
+
return alignment;
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
size_t alignment = 1;
|
|
330
|
+
|
|
331
|
+
FILE * fp{};
|
|
332
|
+
size_t size{};
|
|
242
333
|
};
|
|
243
334
|
|
|
244
|
-
llama_file::llama_file(const char * fname, const char * mode
|
|
335
|
+
llama_file::llama_file(const char * fname, const char * mode, const bool use_direct_io) :
|
|
336
|
+
pimpl(std::make_unique<impl>(fname, mode, use_direct_io)) {}
|
|
245
337
|
llama_file::~llama_file() = default;
|
|
246
338
|
|
|
247
339
|
size_t llama_file::tell() const { return pimpl->tell(); }
|
|
248
340
|
size_t llama_file::size() const { return pimpl->size; }
|
|
249
341
|
|
|
342
|
+
size_t llama_file::read_alignment() const { return pimpl->read_alignment(); }
|
|
343
|
+
|
|
250
344
|
int llama_file::file_id() const {
|
|
251
345
|
#ifdef _WIN32
|
|
252
346
|
return _fileno(pimpl->fp);
|
|
@@ -261,6 +355,7 @@ int llama_file::file_id() const {
|
|
|
261
355
|
|
|
262
356
|
void llama_file::seek(size_t offset, int whence) const { pimpl->seek(offset, whence); }
|
|
263
357
|
void llama_file::read_raw(void * ptr, size_t len) const { pimpl->read_raw(ptr, len); }
|
|
358
|
+
void llama_file::read_raw_at(void * ptr, size_t len, size_t offset) const { pimpl->read_raw_at(ptr, len, offset); }
|
|
264
359
|
|
|
265
360
|
uint32_t llama_file::read_u32() const { return pimpl->read_u32(); }
|
|
266
361
|
|
|
@@ -3,6 +3,7 @@
|
|
|
3
3
|
#include <cstdint>
|
|
4
4
|
#include <memory>
|
|
5
5
|
#include <vector>
|
|
6
|
+
#include <cstdio>
|
|
6
7
|
|
|
7
8
|
struct llama_file;
|
|
8
9
|
struct llama_mmap;
|
|
@@ -13,7 +14,7 @@ using llama_mmaps = std::vector<std::unique_ptr<llama_mmap>>;
|
|
|
13
14
|
using llama_mlocks = std::vector<std::unique_ptr<llama_mlock>>;
|
|
14
15
|
|
|
15
16
|
struct llama_file {
|
|
16
|
-
llama_file(const char * fname, const char * mode);
|
|
17
|
+
llama_file(const char * fname, const char * mode, bool use_direct_io = false);
|
|
17
18
|
~llama_file();
|
|
18
19
|
|
|
19
20
|
size_t tell() const;
|
|
@@ -24,11 +25,14 @@ struct llama_file {
|
|
|
24
25
|
void seek(size_t offset, int whence) const;
|
|
25
26
|
|
|
26
27
|
void read_raw(void * ptr, size_t len) const;
|
|
28
|
+
void read_raw_at(void * ptr, size_t len, size_t offset) const;
|
|
29
|
+
void read_aligned_chunk(size_t offset, void * dest, size_t size) const;
|
|
27
30
|
uint32_t read_u32() const;
|
|
28
31
|
|
|
29
32
|
void write_raw(const void * ptr, size_t len) const;
|
|
30
33
|
void write_u32(uint32_t val) const;
|
|
31
34
|
|
|
35
|
+
size_t read_alignment() const;
|
|
32
36
|
private:
|
|
33
37
|
struct impl;
|
|
34
38
|
std::unique_ptr<impl> pimpl;
|