char-fork/llama.cpp

git clone https://git.t4t.associates/char-fork/llama.cpp

Xuan-Son Nguyenspec : add DFlash2 support (local convolution + candidate selector) (#27342) (#27816)b10f9ca58

char/aliceai-t5-moe
5.2 KiB134 linesraw
1#pragma once
2
3// this is a staging header for new llama.cpp API
4// breaking changes and C++ are allowed. everything here should be considered WIP
5// try as much as possible to not include this header in the rest of the codebase
6
7#include "llama.h"
8
9#include <cstdint>
10#include <map>
11
12// Reserve a new compute graph. It is valid until the next call to llama_graph_reserve.
13LLAMA_API struct ggml_cgraph * llama_graph_reserve(
14        struct llama_context * ctx,
15        uint32_t n_tokens,
16        uint32_t n_seqs,
17        uint32_t n_outputs);
18
19// Get the default ggml_type for a given ftype.
20LLAMA_API ggml_type llama_ftype_get_default_type(llama_ftype ftype);
21
22struct quantize_state_impl;
23
24LLAMA_API quantize_state_impl * llama_quant_init(
25        const llama_model * model,
26        const llama_model_quantize_params * params);
27
28LLAMA_API void llama_quant_free(quantize_state_impl * qs);
29
30// Descriptor for constructing a mock model for quantization testing.
31struct llama_quant_model_desc {
32    const char * architecture;
33    uint32_t n_embd;
34    uint32_t n_ff;
35    uint32_t n_layer;
36    uint32_t n_head;
37    uint32_t n_head_kv;
38    uint32_t n_expert;
39    uint32_t n_embd_head_k;
40    uint32_t n_embd_head_v;
41};
42
43// Create a mock model from a metadata descriptor (for testing).
44// The returned model must be freed with llama_model_free().
45LLAMA_API llama_model * llama_quant_model_from_metadata(const llama_quant_model_desc * desc);
46
47// Returns true if this tensor should be quantized (based on name, dims, params).
48LLAMA_API bool llama_quant_tensor_allows_quantization(
49        const quantize_state_impl * qs,
50        const ggml_tensor * tensor);
51
52// Compute quantization type assignments for a list of tensors.
53// All tensors should be quantizable (use llama_quant_tensor_allows_quantization to filter).
54// result_types: caller-allocated array of n_tensors elements, filled with assigned types.
55LLAMA_API void llama_quant_compute_types(
56        quantize_state_impl * qs,
57        llama_ftype ftype,
58        ggml_tensor ** tensors,
59        ggml_type * result_types,
60        size_t n_tensors);
61
62//
63// device memory querying
64//
65
66// "memory" as in physical memory for a buffer type, in bytes
67struct llama_memory_breakdown_data {
68    size_t model   = 0; // memory allocated for the model
69    size_t context = 0; // memory allocated for the context
70    size_t compute = 0; // memory allocated for temporary compute buffers
71
72    size_t total() const {
73        return model + context + compute;
74    }
75};
76
77struct llama_device_memory_data {
78    int64_t total;
79    int64_t free;
80    llama_memory_breakdown_data mb;
81};
82
83// TODO: convert to C-style data structure
84using llama_memory_breakdown = std::map<ggml_backend_buffer_type_t, llama_memory_breakdown_data>;
85
86LLAMA_API int32_t llama_model_n_expert (const struct llama_model * model);
87LLAMA_API int32_t llama_model_n_devices(const struct llama_model * model);
88
89LLAMA_API ggml_backend_dev_t llama_model_get_device(const struct llama_model * model, int i);
90
91LLAMA_API llama_memory_breakdown llama_get_memory_breakdown(const struct llama_context * ctx);
92
93// Set whether the context outputs nextn embeddings or not
94// If masked == true,  output the embeddings only for the tokens with batch.logits != 0
95// If masked == false, output the embeddings for all tokens in the batch regardless of batch.logits
96LLAMA_API void llama_set_embeddings_nextn(struct llama_context * ctx, bool value, bool masked);
97
98// Select which appended NextN block the DECODER_MTP graph runs (offset past
99// the trunk: il = n_layer() + offset). Used by the speculative NextN driver to
100// chain multiple trained NextN heads. Default 0 (first head).
101LLAMA_API void llama_set_nextn_layer_offset(struct llama_context * ctx, int32_t offset);
102
103// mirrors:
104// LLAMA_API float * llama_get_embeddings(struct llama_context * ctx);
105LLAMA_API float * llama_get_embeddings_nextn(struct llama_context * ctx);
106
107// LLAMA_API float * llama_get_embeddings_ith(struct llama_context * ctx, int32_t i);
108LLAMA_API float * llama_get_embeddings_nextn_ith(struct llama_context * ctx, int32_t i);
109
110// Set whether the context outputs the input embeddings of a specific layer
111LLAMA_API void llama_set_embeddings_layer_inp(struct llama_context * ctx, uint32_t lid, bool value);
112
113// mirrors:
114// LLAMA_API float * llama_get_embeddings(struct llama_context * ctx);
115LLAMA_API float * llama_get_embeddings_layer_inp(struct llama_context * ctx, uint32_t lid);
116
117LLAMA_API llama_context * llama_get_ctx_other(struct llama_context * ctx);
118
119//
120// model/context data extraction
121//
122
123LLAMA_API int32_t llama_model_dflash_selector_top_k(const struct llama_model * model);
124
125// returns pointer to the target-model layer indices
126LLAMA_API const int32_t * llama_model_target_layer_ids  (const struct llama_model * model);
127// returns the number of extracted layers from target model
128LLAMA_API uint32_t        llama_model_target_layer_ids_n(const struct llama_model * model);
129
130// retrieves the whole token embedding matrix in F32 format (n_embd * n_vocab)
131// returns total number of elements or 0 on error
132// if out is nullptr, returns the number of tokens without writing to out
133// caller must allocate enough memory for out before calling
134LLAMA_API uint32_t llama_model_get_tok_embd(const struct llama_model * model, float * out);