Skip to content

Commit 8b273a5

Browse files
authored
Merge branch 'layla-build' into merge
2 parents 6491d6e + a4fa5ef commit 8b273a5

File tree

837 files changed

+170069
-148
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

837 files changed

+170069
-148
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,6 @@ autogen-*.md
7777
!.github/workflows/*.yml
7878

7979
# Models
80-
8180
models/*
8281
models-mnt
8382
!models/.editorconfig
@@ -146,3 +145,4 @@ poetry.toml
146145
# Local scripts
147146
/run-vim.sh
148147
/run-chat.sh
148+
HEXAGON_Tools/

CMakeLists.txt

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,24 @@ set(CMAKE_WARN_UNUSED_CLI YES)
77

88
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
99

10+
if(CMAKE_SYSTEM_NAME STREQUAL "Android")
11+
if(DEFINED HTP_ARCH_VERSION AND NOT CMAKE_BUILD_TYPE STREQUAL "Debug")
12+
if (${HTP_ARCH_VERSION} STREQUAL "v75" OR ${HTP_ARCH_VERSION} STREQUAL "v79")
13+
#works fine on Snapdragon 8Gen3&8Elite with 1.5x - 3x performance gains with the default ggml backend
14+
#set(OPT_FLAG " -O3 -march=armv8.7-a -mcpu=cortex-x1 -mtune=cortex-x1 -ffp-model=fast -fno-finite-math-only")
15+
16+
# this set of flag is more general (without the cortex cpu optimisation, which is only available on very very modern archs)
17+
set(OPT_FLAG " -O3 -flto -D_GNU_SOURCE -fvectorize -ffp-model=fast -fno-finite-math-only")
18+
19+
message("OPT_FLAG:${OPT_FLAG}")
20+
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${DEBUG_FLAG} ${OPT_FLAG}")
21+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${DEBUG_FLAG} ${OPT_FLAG}")
22+
set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} ${DEBUG_FLAG} ${OPT_FLAG}")
23+
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} ${DEBUG_FLAG} ${OPT_FLAG}")
24+
endif()
25+
endif()
26+
endif()
27+
1028
if (NOT XCODE AND NOT MSVC AND NOT CMAKE_BUILD_TYPE)
1129
set(CMAKE_BUILD_TYPE Release CACHE STRING "Build type" FORCE)
1230
set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS "Debug" "Release" "MinSizeRel" "RelWithDebInfo")
@@ -127,6 +145,7 @@ llama_option_depr(WARNING LLAMA_RPC GGML_RPC)
127145
llama_option_depr(WARNING LLAMA_SYCL GGML_SYCL)
128146
llama_option_depr(WARNING LLAMA_SYCL_F16 GGML_SYCL_F16)
129147
llama_option_depr(WARNING LLAMA_CANN GGML_CANN)
148+
llama_option_depr(WARNING LLAMA_HEXAGON GGML_HEXAGON)
130149

131150
if (NOT MSVC)
132151
if (LLAMA_SANITIZE_THREAD)

common/CMakeLists.txt

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,8 @@ llama_add_compile_flags()
66

77
# Build info header
88
#
9-
10-
if(EXISTS "${PROJECT_SOURCE_DIR}/.git")
11-
set(GIT_DIR "${PROJECT_SOURCE_DIR}/.git")
9+
if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/../.git")
10+
set(GIT_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../.git")
1211

1312
# Is git submodule
1413
if(NOT IS_DIRECTORY "${GIT_DIR}")
@@ -18,7 +17,7 @@ if(EXISTS "${PROJECT_SOURCE_DIR}/.git")
1817
if (SLASH_POS EQUAL 0)
1918
set(GIT_DIR "${REAL_GIT_DIR}")
2019
else()
21-
set(GIT_DIR "${PROJECT_SOURCE_DIR}/${REAL_GIT_DIR}")
20+
set(GIT_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../${REAL_GIT_DIR}")
2221
endif()
2322
endif()
2423

@@ -32,6 +31,38 @@ else()
3231
message(WARNING "Git repository not found; to enable automatic generation of build info, make sure Git is installed and the project is a Git repository.")
3332
endif()
3433

34+
# Find Git executable
35+
find_package(Git)
36+
37+
# Get git commit hash
38+
if(GIT_FOUND AND EXISTS "${GIT_DIR}")
39+
execute_process(
40+
COMMAND ${GIT_EXECUTABLE} rev-parse --short HEAD
41+
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
42+
OUTPUT_VARIABLE LLAMA_BUILD_COMMIT
43+
OUTPUT_STRIP_TRAILING_WHITESPACE
44+
ERROR_QUIET
45+
)
46+
47+
# Get build number from git commit count
48+
execute_process(
49+
COMMAND ${GIT_EXECUTABLE} rev-list --count HEAD
50+
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
51+
OUTPUT_VARIABLE LLAMA_BUILD_NUMBER
52+
OUTPUT_STRIP_TRAILING_WHITESPACE
53+
ERROR_QUIET
54+
)
55+
else()
56+
set(LLAMA_BUILD_COMMIT "unknown")
57+
set(LLAMA_BUILD_NUMBER 0)
58+
endif()
59+
60+
# Set compiler info
61+
set(BUILD_COMPILER "${CMAKE_CXX_COMPILER_ID} ${CMAKE_CXX_COMPILER_VERSION}")
62+
63+
# Set build target
64+
set(BUILD_TARGET "${CMAKE_SYSTEM_NAME} ${CMAKE_SYSTEM_PROCESSOR}")
65+
3566
set(TEMPLATE_FILE "${CMAKE_CURRENT_SOURCE_DIR}/build-info.cpp.in")
3667
set(OUTPUT_FILE "${CMAKE_CURRENT_BINARY_DIR}/build-info.cpp")
3768
configure_file(${TEMPLATE_FILE} ${OUTPUT_FILE})

common/common.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1094,6 +1094,9 @@ struct llama_model_params common_model_params_to_llama(common_params & params) {
10941094
auto mparams = llama_model_default_params();
10951095

10961096
if (!params.devices.empty()) {
1097+
// add nullptr to the end just in case
1098+
params.devices.push_back(nullptr);
1099+
10971100
mparams.devices = params.devices.data();
10981101
}
10991102

common/sampling.cpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,17 @@ struct ring_buffer {
6262
return value;
6363
}
6464

65+
T pop_back() {
66+
if (sz == 0) {
67+
throw std::runtime_error("ring buffer is empty");
68+
}
69+
// Move pos backwards, wrapping around if necessary
70+
pos = (pos == 0) ? capacity - 1 : pos - 1;
71+
T value = data[pos];
72+
sz--;
73+
return value;
74+
}
75+
6576
const T & rat(size_t i) const {
6677
if (i >= sz) {
6778
throw std::runtime_error("ring buffer: index out of bounds");
@@ -313,6 +324,12 @@ void common_sampler_reset(struct common_sampler * gsmpl) {
313324
llama_sampler_reset(gsmpl->chain);
314325
}
315326

327+
void common_sampler_reinit_grammar(struct common_sampler * gsmpl, const struct llama_model * model, const char * grammar) {
328+
llama_sampler_reset(gsmpl->grmr);
329+
330+
gsmpl->grmr = llama_sampler_init_grammar(llama_model_get_vocab(model), grammar, "root");
331+
}
332+
316333
struct common_sampler * common_sampler_clone(common_sampler * gsmpl) {
317334
return new common_sampler {
318335
/* .params = */ gsmpl->params,
@@ -466,6 +483,21 @@ std::string common_sampler_prev_str(common_sampler * gsmpl, llama_context * ctx_
466483
return result;
467484
}
468485

486+
const std::vector<llama_token> common_sampler_prev(common_sampler * gsmpl) {
487+
return gsmpl->prev.to_vector();
488+
}
489+
490+
void common_sampler_rollback(common_sampler * gsmpl, int rollback_num) {
491+
if(rollback_num > gsmpl->prev.size()) {
492+
rollback_num = gsmpl->prev.size();
493+
}
494+
495+
// continuously pop the last token
496+
for(int i = 0; i < rollback_num; i++) {
497+
gsmpl->prev.pop_back();
498+
}
499+
}
500+
469501
char common_sampler_type_to_chr(enum common_sampler_type cnstr) {
470502
switch (cnstr) {
471503
case COMMON_SAMPLER_TYPE_DRY: return 'd';

common/sampling.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ void common_sampler_free(struct common_sampler * gsmpl);
4343
// if accept_grammar is true, the token is accepted both by the sampling chain and the grammar
4444
void common_sampler_accept(struct common_sampler * gsmpl, llama_token token, bool accept_grammar);
4545
void common_sampler_reset (struct common_sampler * gsmpl);
46+
void common_sampler_reinit_grammar(struct common_sampler * gsmpl, const struct llama_model * model, const char * grammar);
4647
struct common_sampler * common_sampler_clone (struct common_sampler * gsmpl);
4748

4849
// arguments can be nullptr to skip printing
@@ -96,6 +97,8 @@ std::string common_sampler_print(const struct common_sampler * gsmpl);
9697

9798
// get a string representation of the last accepted tokens
9899
std::string common_sampler_prev_str(common_sampler * gsmpl, llama_context * ctx, int n);
100+
const std::vector<llama_token> common_sampler_prev(common_sampler * gsmpl);
101+
void common_sampler_rollback(common_sampler * gsmpl, int rollback_num);
99102

100103
char common_sampler_type_to_chr(enum common_sampler_type cnstr);
101104
std::string common_sampler_type_to_str(enum common_sampler_type cnstr);

ggml/CMakeLists.txt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,7 @@ option(GGML_OPENCL_EMBED_KERNELS "ggml: embed kernels"
206206
option(GGML_OPENCL_USE_ADRENO_KERNELS "ggml: use optimized kernels for Adreno" ON)
207207
set (GGML_OPENCL_TARGET_VERSION "300" CACHE STRING
208208
"gmml: OpenCL API version to target")
209+
option(GGML_HEXAGON "ggml: use HEXAGON" OFF)
209210

210211
# toolchain for vulkan-shaders-gen
211212
set (GGML_VULKAN_SHADERS_GEN_TOOLCHAIN "" CACHE FILEPATH "ggml: toolchain file for vulkan-shaders-gen")
@@ -270,9 +271,17 @@ set(GGML_PUBLIC_HEADERS
270271
include/ggml-rpc.h
271272
include/ggml-sycl.h
272273
include/ggml-vulkan.h
274+
include/ggml-hexagon.h
273275
include/gguf.h)
274276

275277
set_target_properties(ggml PROPERTIES PUBLIC_HEADER "${GGML_PUBLIC_HEADERS}")
278+
279+
# link android log library
280+
if(ANDROID)
281+
find_library(log-lib log)
282+
target_link_libraries(ggml PRIVATE ${log-lib})
283+
endif()
284+
276285
#if (GGML_METAL)
277286
# set_target_properties(ggml PROPERTIES RESOURCE "${CMAKE_CURRENT_SOURCE_DIR}/src/ggml-metal.metal")
278287
#endif()

ggml/include/ggml-backend.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,7 @@ extern "C" {
202202
//
203203
// Backend registry
204204
//
205+
GGML_API void ggml_backend_reg_layla(bool useVulkan, bool useOpenCL, bool useHexagon, bool useMetal);
205206

206207
GGML_API void ggml_backend_device_register(ggml_backend_dev_t device);
207208

ggml/include/ggml-hexagon.h

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* Copyright (c) 2024-2025 The ggml authors
3+
*/
4+
#pragma once
5+
6+
#include "ggml.h"
7+
#include "ggml-backend.h"
8+
9+
#ifdef __cplusplus
10+
extern "C" {
11+
#endif
12+
13+
#define GGML_HEXAGON_MAX_DEVICES 4
14+
#define GGML_HEXAGON_BACKEND_NAME "hexagon"
15+
16+
enum HEXAGONBackend {
17+
HEXAGON_BACKEND_QNNCPU = 0,
18+
HEXAGON_BACKEND_QNNGPU = 1,
19+
HEXAGON_BACKEND_QNNNPU = 2,
20+
HEXAGON_BACKEND_CDSP = 3,
21+
HEXAGON_BACKEND_GGML = 4, //"fake" HEXAGON backend for compare performance between HEXAGON backend and ggml backend
22+
};
23+
24+
GGML_BACKEND_API ggml_backend_t ggml_backend_hexagon_init(size_t dev_num, const char * qnn_lib_path);
25+
26+
GGML_BACKEND_API bool ggml_backend_is_hexagon(ggml_backend_t backend);
27+
28+
GGML_BACKEND_API int ggml_backend_hexagon_get_device_count(void);
29+
30+
GGML_BACKEND_API ggml_backend_reg_t ggml_backend_hexagon_reg(void);
31+
32+
const char * ggml_backend_hexagon_get_devname(size_t dev_num);
33+
34+
#ifdef __cplusplus
35+
}
36+
#endif

ggml/include/ggml.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -659,6 +659,7 @@ extern "C" {
659659

660660
// accepts a UTF-8 path, even on Windows
661661
GGML_API FILE * ggml_fopen(const char * fname, const char * mode);
662+
GGML_API FILE * ggml_fdopen(int fd, const char * mode, size_t fd_offset);
662663

663664
GGML_API void ggml_print_object (const struct ggml_object * obj);
664665
GGML_API void ggml_print_objects(const struct ggml_context * ctx);

0 commit comments

Comments
 (0)