Skip to content

Commit b407451

Browse files
authored
Merge branch 'ggml-org:master' into master
2 parents 73923f6 + b3e1666 commit b407451

File tree

23 files changed

+1775
-1267
lines changed

23 files changed

+1775
-1267
lines changed

.devops/cloud-v-pipeline

Lines changed: 0 additions & 22 deletions
This file was deleted.
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
name: Build on RISCV Linux Machine by Cloud-V
2+
on:
3+
workflow_dispatch:
4+
workflow_call:
5+
6+
jobs:
7+
bianbu-riscv64-native: # Bianbu 2.2
8+
runs-on: self-hosted
9+
10+
steps:
11+
- name: Install prerequisites
12+
run: |
13+
sudo apt-get update || true
14+
sudo apt-get install -y libatomic1
15+
- uses: actions/checkout@v4
16+
- name: Setup Riscv
17+
run: |
18+
sudo apt-get update || true
19+
sudo apt-get install -y --no-install-recommends \
20+
build-essential \
21+
gcc-14-riscv64-linux-gnu \
22+
g++-14-riscv64-linux-gnu \
23+
cmake
24+
25+
- name: Build
26+
run: |
27+
cmake -B build -DLLAMA_CURL=OFF \
28+
-DCMAKE_BUILD_TYPE=Release \
29+
-DGGML_OPENMP=OFF \
30+
-DLLAMA_BUILD_EXAMPLES=ON \
31+
-DLLAMA_BUILD_TOOLS=ON \
32+
-DLLAMA_BUILD_TESTS=OFF \
33+
-DCMAKE_SYSTEM_NAME=Linux \
34+
-DCMAKE_SYSTEM_PROCESSOR=riscv64 \
35+
-DCMAKE_C_COMPILER=riscv64-linux-gnu-gcc-14 \
36+
-DCMAKE_CXX_COMPILER=riscv64-linux-gnu-g++-14 \
37+
-DCMAKE_POSITION_INDEPENDENT_CODE=ON \
38+
-DCMAKE_FIND_ROOT_PATH=/usr/lib/riscv64-linux-gnu \
39+
-DCMAKE_FIND_ROOT_PATH_MODE_PROGRAM=NEVER \
40+
-DCMAKE_FIND_ROOT_PATH_MODE_LIBRARY=ONLY \
41+
-DCMAKE_FIND_ROOT_PATH_MODE_INCLUDE=BOTH
42+
43+
cmake --build build --config Release -j $(nproc)
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
name: "Copilot Setup Steps"
2+
3+
# Automatically run the setup steps when they are changed to allow for easy validation, and
4+
# allow manual testing through the repository's "Actions" tab
5+
on:
6+
workflow_dispatch:
7+
push:
8+
paths:
9+
- .github/workflows/copilot-setup-steps.yml
10+
pull_request:
11+
paths:
12+
- .github/workflows/copilot-setup-steps.yml
13+
14+
jobs:
15+
# The job MUST be called `copilot-setup-steps` or it will not be picked up by Copilot.
16+
copilot-setup-steps:
17+
runs-on: ubuntu-latest
18+
19+
# Set the permissions to the lowest permissions possible needed for your steps.
20+
# Copilot will be given its own token for its operations.
21+
permissions:
22+
# If you want to clone the repository as part of your setup steps, for example to install dependencies, you'll need the `contents: read` permission. If you don't clone the repository in your setup steps, Copilot will do this for you automatically after the steps complete.
23+
contents: read
24+
25+
# You can define any steps you want, and they will run before the agent starts.
26+
# If you do not check out your code, Copilot will do this for you.
27+
steps:
28+
- name: Checkout code
29+
uses: actions/checkout@v4
30+
31+
- name: ccache
32+
uses: hendrikmuhs/[email protected]
33+
with:
34+
key: copilot-setup-steps
35+
evict-old-files: 1d
36+
37+
- name: Dependencies
38+
id: depends
39+
run: |
40+
sudo apt-get update
41+
sudo apt-get install build-essential libcurl4-openssl-dev
42+
43+
- name: Set up Python
44+
uses: actions/setup-python@v5
45+
with:
46+
python-version: '3.11'
47+
48+
- name: Install Python dependencies
49+
run: |
50+
python3 -m venv .venv
51+
.venv/bin/activate
52+
pip install -r requirements/requirements-all.txt -r tools/server/tests/requirements.txt
53+
pip install flake8 pyright

common/arg.cpp

Lines changed: 67 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -749,6 +749,39 @@ std::pair<long, std::vector<char>> common_remote_get_content(const std::string &
749749
// utils
750750
//
751751

752+
// Helper function to parse tensor buffer override strings
753+
static void parse_tensor_buffer_overrides(const std::string & value, std::vector<llama_model_tensor_buft_override> & overrides) {
754+
std::map<std::string, ggml_backend_buffer_type_t> buft_list;
755+
for (size_t i = 0; i < ggml_backend_dev_count(); ++i) {
756+
auto * dev = ggml_backend_dev_get(i);
757+
auto * buft = ggml_backend_dev_buffer_type(dev);
758+
if (buft) {
759+
buft_list[ggml_backend_buft_name(buft)] = buft;
760+
}
761+
}
762+
763+
for (const auto & override : string_split<std::string>(value, ',')) {
764+
std::string::size_type pos = override.find('=');
765+
if (pos == std::string::npos) {
766+
throw std::invalid_argument("invalid value");
767+
}
768+
std::string tensor_name = override.substr(0, pos);
769+
std::string buffer_type = override.substr(pos + 1);
770+
771+
if (buft_list.find(buffer_type) == buft_list.end()) {
772+
printf("Available buffer types:\n");
773+
for (const auto & it : buft_list) {
774+
printf(" %s\n", ggml_backend_buft_name(it.second));
775+
}
776+
throw std::invalid_argument("unknown buffer type");
777+
}
778+
// keep strings alive and avoid leaking memory by storing them in a static vector
779+
static std::list<std::string> buft_overrides;
780+
buft_overrides.push_back(tensor_name);
781+
overrides.push_back({buft_overrides.back().c_str(), buft_list.at(buffer_type)});
782+
}
783+
}
784+
752785
struct handle_model_result {
753786
bool found_mmproj = false;
754787
common_params_model mmproj;
@@ -993,6 +1026,10 @@ static bool common_params_parse_ex(int argc, char ** argv, common_params_context
9931026
params.tensor_buft_overrides.push_back({nullptr, nullptr});
9941027
}
9951028

1029+
if (!params.speculative.tensor_buft_overrides.empty()) {
1030+
params.speculative.tensor_buft_overrides.push_back({nullptr, nullptr});
1031+
}
1032+
9961033
if (!params.chat_template.empty() && !common_chat_verify_template(params.chat_template, params.use_jinja)) {
9971034
throw std::runtime_error(string_format(
9981035
"error: the supplied chat template is not supported: %s%s\n",
@@ -2349,40 +2386,15 @@ common_params_context common_params_parser_init(common_params & params, llama_ex
23492386
add_opt(common_arg(
23502387
{"--override-tensor", "-ot"}, "<tensor name pattern>=<buffer type>,...",
23512388
"override tensor buffer type", [](common_params & params, const std::string & value) {
2352-
/* static */ std::map<std::string, ggml_backend_buffer_type_t> buft_list;
2353-
if (buft_list.empty()) {
2354-
// enumerate all the devices and add their buffer types to the list
2355-
for (size_t i = 0; i < ggml_backend_dev_count(); ++i) {
2356-
auto * dev = ggml_backend_dev_get(i);
2357-
auto * buft = ggml_backend_dev_buffer_type(dev);
2358-
if (buft) {
2359-
buft_list[ggml_backend_buft_name(buft)] = buft;
2360-
}
2361-
}
2362-
}
2363-
2364-
for (const auto & override : string_split<std::string>(value, ',')) {
2365-
std::string::size_type pos = override.find('=');
2366-
if (pos == std::string::npos) {
2367-
throw std::invalid_argument("invalid value");
2368-
}
2369-
std::string tensor_name = override.substr(0, pos);
2370-
std::string buffer_type = override.substr(pos + 1);
2371-
2372-
if (buft_list.find(buffer_type) == buft_list.end()) {
2373-
printf("Available buffer types:\n");
2374-
for (const auto & it : buft_list) {
2375-
printf(" %s\n", ggml_backend_buft_name(it.second));
2376-
}
2377-
throw std::invalid_argument("unknown buffer type");
2378-
}
2379-
// keep strings alive and avoid leaking memory by storing them in a static vector
2380-
static std::list<std::string> buft_overrides;
2381-
buft_overrides.push_back(tensor_name);
2382-
params.tensor_buft_overrides.push_back({buft_overrides.back().c_str(), buft_list.at(buffer_type)});
2383-
}
2389+
parse_tensor_buffer_overrides(value, params.tensor_buft_overrides);
23842390
}
23852391
));
2392+
add_opt(common_arg(
2393+
{"--override-tensor-draft", "-otd"}, "<tensor name pattern>=<buffer type>,...",
2394+
"override tensor buffer type for draft model", [](common_params & params, const std::string & value) {
2395+
parse_tensor_buffer_overrides(value, params.speculative.tensor_buft_overrides);
2396+
}
2397+
).set_examples({LLAMA_EXAMPLE_SPECULATIVE, LLAMA_EXAMPLE_SERVER}));
23862398
add_opt(common_arg(
23872399
{"--cpu-moe", "-cmoe"},
23882400
"keep all Mixture of Experts (MoE) weights in the CPU",
@@ -2405,6 +2417,27 @@ common_params_context common_params_parser_init(common_params & params, llama_ex
24052417
}
24062418
}
24072419
).set_env("LLAMA_ARG_N_CPU_MOE"));
2420+
add_opt(common_arg(
2421+
{"--cpu-moe-draft", "-cmoed"},
2422+
"keep all Mixture of Experts (MoE) weights in the CPU for the draft model",
2423+
[](common_params & params) {
2424+
params.speculative.tensor_buft_overrides.push_back({"\\.ffn_(up|down|gate)_exps", ggml_backend_cpu_buffer_type()});
2425+
}
2426+
).set_examples({LLAMA_EXAMPLE_SPECULATIVE, LLAMA_EXAMPLE_SERVER}).set_env("LLAMA_ARG_CPU_MOE_DRAFT"));
2427+
add_opt(common_arg(
2428+
{"--n-cpu-moe-draft", "-ncmoed"}, "N",
2429+
"keep the Mixture of Experts (MoE) weights of the first N layers in the CPU for the draft model",
2430+
[](common_params & params, int value) {
2431+
if (value < 0) {
2432+
throw std::invalid_argument("invalid value");
2433+
}
2434+
for (int i = 0; i < value; ++i) {
2435+
static std::list<std::string> buft_overrides_draft;
2436+
buft_overrides_draft.push_back(string_format("blk\\.%d\\.ffn_(up|down|gate)_exps", i));
2437+
params.speculative.tensor_buft_overrides.push_back({buft_overrides_draft.back().c_str(), ggml_backend_cpu_buffer_type()});
2438+
}
2439+
}
2440+
).set_examples({LLAMA_EXAMPLE_SPECULATIVE, LLAMA_EXAMPLE_SERVER}).set_env("LLAMA_ARG_N_CPU_MOE_DRAFT"));
24082441
add_opt(common_arg(
24092442
{"-ngl", "--gpu-layers", "--n-gpu-layers"}, "N",
24102443
"number of layers to store in VRAM",
@@ -3130,7 +3163,7 @@ common_params_context common_params_parser_init(common_params & params, llama_ex
31303163
params.speculative.cpuparams.n_threads = std::thread::hardware_concurrency();
31313164
}
31323165
}
3133-
).set_examples({LLAMA_EXAMPLE_SPECULATIVE}));
3166+
).set_examples({LLAMA_EXAMPLE_SPECULATIVE, LLAMA_EXAMPLE_SERVER}));
31343167
add_opt(common_arg(
31353168
{"-tbd", "--threads-batch-draft"}, "N",
31363169
"number of threads to use during batch and prompt processing (default: same as --threads-draft)",
@@ -3140,7 +3173,7 @@ common_params_context common_params_parser_init(common_params & params, llama_ex
31403173
params.speculative.cpuparams_batch.n_threads = std::thread::hardware_concurrency();
31413174
}
31423175
}
3143-
).set_examples({LLAMA_EXAMPLE_SPECULATIVE}));
3176+
).set_examples({LLAMA_EXAMPLE_SPECULATIVE, LLAMA_EXAMPLE_SERVER}));
31443177
add_opt(common_arg(
31453178
{"-Cd", "--cpu-mask-draft"}, "M",
31463179
"Draft model CPU affinity mask. Complements cpu-range-draft (default: same as --cpu-mask)",

common/common.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,7 @@ struct common_params_speculative {
202202
float p_split = 0.1f; // speculative decoding split probability
203203
float p_min = 0.75f; // minimum speculative decoding probability (greedy)
204204
std::vector<std::pair<std::string, std::string>> replacements; // main to speculative model replacements
205+
std::vector<llama_model_tensor_buft_override> tensor_buft_overrides;
205206

206207
ggml_type cache_type_k = GGML_TYPE_F16; // KV cache data type for the K
207208
ggml_type cache_type_v = GGML_TYPE_F16; // KV cache data type for the V

examples/speculative-simple/speculative-simple.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ int main(int argc, char ** argv) {
5959
}
6060

6161
params.cpuparams_batch.n_threads = params.speculative.cpuparams_batch.n_threads;
62+
params.tensor_buft_overrides = params.speculative.tensor_buft_overrides;
63+
6264
common_init_result llama_init_dft = common_init_from_params(params);
6365

6466
//model_dft = llama_init_dft.model.get();

examples/speculative/speculative.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,8 @@ int main(int argc, char ** argv) {
8585
}
8686

8787
params.cpuparams_batch.n_threads = params.speculative.cpuparams_batch.n_threads;
88+
params.tensor_buft_overrides = params.speculative.tensor_buft_overrides;
89+
8890
common_init_result llama_init_dft = common_init_from_params(params);
8991

9092
model_dft = llama_init_dft.model.get();

ggml/include/ggml.h

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,8 @@
241241
#define GGML_ROPE_TYPE_MROPE 8
242242
#define GGML_ROPE_TYPE_VISION 24
243243

244+
#define GGML_MROPE_SECTIONS 4
245+
244246
#define GGML_UNUSED(x) (void)(x)
245247

246248
#define GGML_PAD(x, n) (((x) + (n) - 1) & ~((n) - 1))
@@ -1660,7 +1662,7 @@ extern "C" {
16601662
struct ggml_tensor * b,
16611663
struct ggml_tensor * c,
16621664
int n_dims,
1663-
int sections[4],
1665+
int sections[GGML_MROPE_SECTIONS],
16641666
int mode,
16651667
int n_ctx_orig,
16661668
float freq_base,
@@ -1686,6 +1688,22 @@ extern "C" {
16861688
float beta_fast,
16871689
float beta_slow);
16881690

1691+
GGML_API struct ggml_tensor * ggml_rope_multi_inplace(
1692+
struct ggml_context * ctx,
1693+
struct ggml_tensor * a,
1694+
struct ggml_tensor * b,
1695+
struct ggml_tensor * c,
1696+
int n_dims,
1697+
int sections[GGML_MROPE_SECTIONS],
1698+
int mode,
1699+
int n_ctx_orig,
1700+
float freq_base,
1701+
float freq_scale,
1702+
float ext_factor,
1703+
float attn_factor,
1704+
float beta_fast,
1705+
float beta_slow);
1706+
16891707
GGML_DEPRECATED(GGML_API struct ggml_tensor * ggml_rope_custom(
16901708
struct ggml_context * ctx,
16911709
struct ggml_tensor * a,

0 commit comments

Comments
 (0)